gpt4 book ai didi

ruby-on-rails - 如何在 RSpec 规范中 stub 闪存?

转载 作者:数据小太阳 更新时间:2023-10-29 07:49:51 33 4
gpt4 key购买 nike

在我看来,我有一个hidden_​​field_tag,其值是在 Controller 中设置的flash。也就是说,流程是这样的:

Controller :

def home
flash[:id] = 123
end

查看:

<% form_tag(new_invitee_path) %>
<%= hidden_field_tag :referer, flash[:id] %>
<% end %>

提交给 new_invitee_path 的参数:

{ "referer" => "123" }

我可以确认在手动测试中这可以正常工作,但我不知道如何适本地 stub 。

在我的测试中我有:

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

最佳答案

您的规范是功能规范,因此规范环境无法访问闪存之类的东西。不要尝试直接使用闪光灯。相反,理想情况下,如果 flash 值设置为应有的方式,则测试用户对应用程序的看法是否符合应有的方式。我不会只测试表单中是否存在隐藏字段;我会测试它是否具有提交表单后应有的效果。这就是功能规范的全部内容:从用户的角度测试应用程序作为一个整体工作。

如果 flash 值从未在 UI 中使用过,只是记录或存储在数据库中,则可以测试日志行或模型对象是否具有存储在 flash 中的值。 (此处的用户是稍后会查看日志或其他内容的管理员。)但如果闪存确实影响了 UI,则最好进行测试。

关于ruby-on-rails - 如何在 RSpec 规范中 stub 闪存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25919458/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com