如何在 Rails 3 中测试 cookies.permanent.signed [英] How to test cookies.permanent.signed in Rails 3
问题描述
我在某个控制器中有一个动作,它在一个永久签名的 cookie 中设置了一些值,如下所示:
<前><代码>def some_actioncookies.permanent.signed[:cookie_name] = "somevalue"结尾在一些功能测试中,我试图测试 cookie 是否设置正确,起诉这个:
<前><代码>测试测试饼干"做assert_equal "somevalue", cookies.permanent.signed[:cookie_name]结尾但是,当我运行测试时,出现以下错误:
<前><代码>NoMethodError: # 的未定义方法永久"如果我只尝试:
<前><代码>测试测试饼干"做assert_equal "somevalue", cookies.signed[:cookie_name]结尾我得到:
<前><代码>NoMethodError: 未定义的方法已签名" #如何在 Rails 3 中测试签名 cookie?
我在谷歌搜索类似问题的解决方案时遇到了这个问题,所以我会在这里发布.我希望在测试控制器操作之前在 Rspec 中设置一个签名的 cookie.以下工作:
jar = ActionDispatch::Cookies::CookieJar.build(@request)jar.signed[:some_key] = "某个值"@request.cookies['some_key'] = jar[:some_key]得到:显示...
请注意,以下方法无效:
# 无效;控制器没有看到签名的 cookie@request.cookie_jar.signed[:some_key] = "某个值"得到:显示...
I have a action in some controller that set some value in a permanent signed cookie like this:
def some_action
cookies.permanent.signed[:cookie_name] = "somevalue"
end
And in some functional test, I'm trying to test if the cookie was set correctly suing this:
test "test cookies" do
assert_equal "somevalue", cookies.permanent.signed[:cookie_name]
end
However, when I run the test, I got the following error:
NoMethodError: undefined method `permanent' for #
If I try only:
test "test cookies" do
assert_equal "somevalue", cookies.signed[:cookie_name]
end
I get:
NoMethodError: undefined method `signed' for #
How to test signed cookies in Rails 3?
I came across this question while Googling for a solution to a similar issue, so I'll post here. I was hoping to set a signed cookie in Rspec before testing a controller action. The following worked:
jar = ActionDispatch::Cookies::CookieJar.build(@request)
jar.signed[:some_key] = "some value"
@request.cookies['some_key'] = jar[:some_key]
get :show ...
Note that the following didn't work:
# didn't work; the controller didn't see the signed cookie
@request.cookie_jar.signed[:some_key] = "some value"
get :show ...
这篇关于如何在 Rails 3 中测试 cookies.permanent.signed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!