【发布时间】:2014-09-18 18:04:00
【问题描述】:
在我看来,我在控制器中设置了一个hidden_field_tag,其值为flash。也就是说,流程如下:
控制器:
def home
flash[:id] = 123
end
查看:
<% form_tag(new_invitee_path) %>
<%= hidden_field_tag :referer, flash[:id] %>
<% end %>
提交给 new_invitee_path 的参数:
{ "referer" => "123" }
我可以确认在手动测试中这可以正常工作,但我不知道如何正确地存根。
在我的测试中我有:
before do
#set flash
visit '/home'
fill_in "rest_of_form"
click_button "submit_form
end
以下是我尝试为set flash 做的事情以及我收到的错误消息:
flash[:id] = 123
# OR
flash.now[:id] = 123
# both render error: undefined local variable or method `flash' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fc1040f7d60>
# Have also tried a tactic found online to set flash for response object like this:
visit '/home'
response.flash[:id] = 123
# OR
response.flash.now[:id] = 123
# both render error: undefined local variable or method `response' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fe118a38490>
#Have read online that it's a problem with the flash being sweeped, so I tried to stub out the sweep, but am unclear how to set the anonymous controller or whatever correctly
controller.instance_eval{flash.stub!(:sweep)}
flash[:id] = 123
# OR
flash.now[:id] = 123
# renders error: undefined local variable or method `flash' for nil:NilClass
【问题讨论】:
标签: ruby-on-rails ruby unit-testing ruby-on-rails-4 rspec