Ruby on rails 如何测试接受RSpec块的Rails助手?
进行以下RSpec测试:Ruby on rails 如何测试接受RSpec块的Rails助手?,ruby-on-rails,rspec,block,helpers,Ruby On Rails,Rspec,Block,Helpers,进行以下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
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
如果为按钮辅助对象指定了块,则该辅助对象应在单击后创建按钮和可展开菜单
示例视图:
= 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测试,它就失败了,在进一步检查后,它似乎在测试中没有任何结果
<div class="box">...</div>
。。。
RSpec输出:
expected "<li class=\"expandable menu-item\"><a href=\"#\"><span>Hello</span></a><div class=\"box\"></div></li>" to include "Test"
期望“”包含“测试”
我尝试过在block_给出的情况下提升内部内容?在content=with_output_buffer(&block)之后,它确实是空的。我一定是在考试中做错了什么,为什么它是空的
非常感谢您的帮助!:) 好的,我试试:-)
您是否有任何特别的理由将与_output_buffer
一起使用,而不是使用捕获
您可以查看capture
源代码:…/gems/actionpack-3.0.3/lib/action\u view/helpers/capture\u helper.rb
,您将看到它以不同的方式使用和\u output\u buffer
要点是capture
可以处理返回字符串的块。因此,您可以简单地使用:
it "should work" do
helper.button("Hello", "#") do
"test"
end.should include "test"
end
更新:啊,我忘了提一下,当您从以下位置更改代码时,它会起作用:
content = with_output_buffer(&block)
到
谢谢你,伙计!它似乎与捕获有关。我会看看捕获的来源,看看它能做什么。
content = with_output_buffer(&block)
content = capture(&block)