在我看來,我有一個hidden_field_tag
,其值是在控制器中flash
集。換句話說,流程如下:如何在RSpec規範中對閃存進行存根?
控制器:
def home
flash[:id] = 123
end
檢視:
<% form_tag(new_invitee_path) %>
<%= hidden_field_tag :referer, flash[:id] %>
<% end %>
提交new_invitee_pathPARAMS:
{ "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
確定這是公平的,在這種情況下,我想,這樣它傳遞給隱藏的值設置爲123本身,我可以檢查它的保存到新對象中。那麼在RSPEC有沒有辦法做到這一點? – james 2014-09-18 19:31:32
它真的只是一個硬編碼的數字嗎?如果是這樣,只需將其移動到一個常量並在測試中引用該常量即可。如果它真的來自一個方法或東西,你可以存根的方法,或以其他方式安排在試驗東西,所以該方法返回一個你選擇,你可以在最後斷言。 – 2014-09-19 01:33:45