如何测试使用 RSpec 接受块的 Rails 助手?
给定以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我会尝试:-)
您有什么特殊原因使用
with_output_buffer
而不是capture
吗?您可以查看
capture
源代码:..../gems/actionpack-3.0.3/lib/action_view/helpers/capture_helper.rb
,您会看到它使用with_output_buffer
的方式与您使用它的方式不同。要点是
capture
可以与返回字符串的块一起使用。因此,您可以简单地使用:更新:啊,我忘了提到,当您将代码从:更改
为时,它就会起作用
ok, i'll try :-)
do you have any particular reason to use
with_output_buffer
instead ofcapture
?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 usingwith_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:UPDATE: Ah I forgot to mention that it will work when you change your code from:
to