测试操作时如何在 Flash 中输入值
我正在尝试测试需要存储在闪存中的值的操作。
def my_action
if flash[:something].nil?
redirect_to root_path if flash[:something]
return
end
# Do some other stuff
end
在我的测试中,我做了类似的事情:
before(:each) do
flash[:something] = "bob"
end
it "should do whatever I had commented out above" do
get :my_action
# Assert something
end
我遇到的问题是 flash 在 my_action 中没有值。我猜这是因为实际上没有发生任何请求。
有没有办法为这样的测试设置闪存?
I'm trying to test an action that needs a value stored in flash.
def my_action
if flash[:something].nil?
redirect_to root_path if flash[:something]
return
end
# Do some other stuff
end
In my test I do something like:
before(:each) do
flash[:something] = "bob"
end
it "should do whatever I had commented out above" do
get :my_action
# Assert something
end
The problem I'm running into is that flash has no values inside of my_action. I'm guessing this is because no request actually happens.
Is there a way to set flash up for a test like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我必须解决类似的问题;
我有一个控制器操作,根据哈希条目的值,在完成时重定向到两个路径之一。
对于上面的示例,我发现有效的规范测试是:
@current_session 是具有会话特定内容的哈希;我正在使用 authlogic。
我在[测试 Rails 应用程序指南[1]< 中找到了关于使用 get 的第四个参数的 flash /a>)。我发现同样的方法也适用于删除;我想其他人也是如此。
I had to solve a similar issue;
I had a controller action that redirected to one of two paths at completion depending of the value of a hash entry.
The spec test that I found worked, for your example above, was:
@current_session is a hash with session specific stuf; I'm using authlogic.
I found about using the fourth argument of get for the flash in [A Guide to Testing Rails Applications[1]). I found that the same approach also works for delete; and I presume all others.
以下内容对我来说适用于 RoR 4.1:
the following worked for me with RoR 4.1:
问题是,按照您的方式使用闪存哈希意味着它只能用于下一个请求。为了将闪存哈希设置为测试的值,您可以编写如下内容:
这确保您可以检查操作的逻辑。因为它现在将正确设置闪存哈希,所以输入您的 my_action 并对闪存哈希执行检查。
The problem is that using the flash hash the way you do means it only becomes avaialble for the next request. In order to set the flash hash to a value for your test, you could write something like this:
This ensures that you can check the logic of your action. Because it will now set the flash hash properly, enter your my_action and perform the check on the flash hash.