如何使用 rspec 测试签名或永久 cookie?
我可以将 cookie 写入请求中:
request.cookies['foo'] = 'bar'
但是这些都不起作用:
request.cookies.permanent['foo'] = 'bar'
request.cookies.signed['foo'] = 'bar'
request.cookies.permanent.signed['foo'] = 'bar' # what I really want
我收到空的哈希消息,例如这样的消息:
NoMethodError:
undefined method `signed' for {}:Hash
如何为我的测试创建这些 cookie?
我使用的是 Rails 3.1 和 rspec 2.6.0。
I can write cookies to the request fine with:
request.cookies['foo'] = 'bar'
But none of these work:
request.cookies.permanent['foo'] = 'bar'
request.cookies.signed['foo'] = 'bar'
request.cookies.permanent.signed['foo'] = 'bar' # what I really want
I get empty hash messages like messages like this:
NoMethodError:
undefined method `signed' for {}:Hash
How can I create these cookies for my tests?
I am using rails 3.1 and rspec 2.6.0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
替代
Use
Instead
假设您有以下 Rails 助手:
要使用 RSpec 2.11.0 在 Rails 3.2.6 中测试它,您可以执行以下操作:
我从未遇到过使用问题rspec 来测试
cookies.signed[:foo].should == 'bar'
,但是调用cookies.permanent
在过去给我带来了问题。上面,我实际上只是对permanent
方法进行存根操作并再次返回cookies
对象。这使我可以测试它是否已被调用。您确实不必担心天气导轨本身设置永久 cookie,因为它已经过测试。
Say you have the following Rails helper:
To test this in Rails 3.2.6 using RSpec 2.11.0, you could do the following:
I've never run into problems using rspec to test
cookies.signed[:foo].should == 'bar'
, but throwing in a call tocookies.permanent
has given me problems in the past. Above, I'm really just stubbing thepermanent
method and returning thecookies
object again. This allows me to test that it has been called.You really shouldn't have to worry about weather rails itself set a permanent cookie because that has been tested already.