如何测试使用 RSpec 接受块的 Rails 助手?

发布于 2024-10-11 06:15:33 字数 1581 浏览 2 评论 0原文

给定以下 RSpec 测试:

  context "items" do
    it "should be able to place links in an expandable menu" do
      output = helper.button("Hello", '#') do
        self.content << helper.item("Test", "example.org")
      end.should include "Test"
    end
  end

以及以下助手:

 def button(name, url, options = {}, &block)
    if block_given?
      content = with_output_buffer(&block)
      content_tag(:li, :class => 'expandable menu-item') do
        concat link_to(content_tag(:span, name), url, options)
        concat content_tag :div, content, :class => :box
      end
    else
      return content_tag :li, link_to(content_tag(:span, name), url, options), :class => :button
    end
  end

如果为按钮助手提供了块,则单击后该助手应该创建一个按钮和一个可扩展菜单。

示例视图:

= menu do
  = button("Test", '#') do
    %h1 Hello you!

产生:

<li class="expandable menu-item"><a href="#"><span>Test</span></a><div class="box"><h1>Hello you!</h1> 

正是我所期望的!当我尝试 RSpec 测试时,它失败了,经过进一步检查,RSpec 输出中似乎没有产生任何结果

<div class="box">...</div>

expected "<li class=\"expandable menu-item\"><a href=\"#\"><span>Hello</span></a><div class=\"box\"></div></li>" to include "Test"

我尝试在 if block_given? 中提高内容?在 content = with_output_buffer(&block) 之后,它确实是空的。我在测试中一定做错了什么,为什么它是空的。

非常感谢您的帮助! :)

Given the following RSpec test:

  context "items" do
    it "should be able to place links in an expandable menu" do
      output = helper.button("Hello", '#') do
        self.content << helper.item("Test", "example.org")
      end.should include "Test"
    end
  end

And the following helper:

 def button(name, url, options = {}, &block)
    if block_given?
      content = with_output_buffer(&block)
      content_tag(:li, :class => 'expandable menu-item') do
        concat link_to(content_tag(:span, name), url, options)
        concat content_tag :div, content, :class => :box
      end
    else
      return content_tag :li, link_to(content_tag(:span, name), url, options), :class => :button
    end
  end

This helper is supposed to create a button and an expandable menu once clicked if a block is given to the button helper.

Example View:

= menu do
  = button("Test", '#') do
    %h1 Hello you!

Produces:

<li class="expandable menu-item"><a href="#"><span>Test</span></a><div class="box"><h1>Hello you!</h1> 

Exactly what I expected! Once I try out the RSpec test it fails and upon further inspected it seems nothing is being yielded within the

<div class="box">...</div>

RSpec output:

expected "<li class=\"expandable menu-item\"><a href=\"#\"><span>Hello</span></a><div class=\"box\"></div></li>" to include "Test"

I've tried raising content inside if block_given? after content = with_output_buffer(&block) and it is indeed empty. I must be doing something wrong in my test as to why it's empty.

Help would be much appreciated! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿光 2024-10-18 06:15:33

好的,我会尝试:-)

您有什么特殊原因使用 with_output_buffer 而不是 capture 吗?

您可以查看capture源代码:..../gems/actionpack-3.0.3/lib/action_view/helpers/capture_helper.rb,您会看到它使用 with_output_buffer 的方式与您使用它的方式不同。

要点是capture可以与返回字符串的块一起使用。因此,您可以简单地使用:

it "should work" do
  helper.button("Hello", "#") do
    "test"
  end.should include "test"
end

更新:啊,我忘了提到,当您将代码从:更改

content = with_output_buffer(&block)

为时,它就会起作用

content = capture(&block)

ok, i'll try :-)

do you have any particular reason to use with_output_buffer instead of capture?

You can take look at the capture source: ..../gems/actionpack-3.0.3/lib/action_view/helpers/capture_helper.rb and you will see that it is using with_output_buffer in a different way that you are using it.

The gist is that capture can work with blocks that returns string. So with that you can simply use:

it "should work" do
  helper.button("Hello", "#") do
    "test"
  end.should include "test"
end

UPDATE: Ah I forgot to mention that it will work when you change your code from:

content = with_output_buffer(&block)

to

content = capture(&block)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文