测试使用 cookie 的 Rails 助手
我在 app/helpers/application_helper.rb
中有一个调用 cookies
的方法。这是完全允许的;这个方法在我的开发环境中运行得非常好,并且知道我说的 cookie 的意思。
也会出现错误
NoMethodError: undefined method `cookies' for nil:NilClass
我正在使用 TestUnit 测试此帮助程序(在 test/unit/helpers/application_helper_test.rb
中),即使对于四个不设置 cookie 的测试, 。这与需要“使用 cookie_jar
在测试中设置 cookie”无关,因为问题不是我设置 cookie 而我的助手无法识别它们,问题是正在调用的对象 cookie
为nil。
我不确定正在调用哪个控制器对象 cookies
,因此我对它们进行了 p
全部检查,发现来自 @controller
、@request
和 @response
,只有 @response
为 nil。所以我尝试将其设置为“牛”。确实如此!我的错误发生了变化:
NoMethodError: undefined method `cookies' for "cow":String
所以看来我只需要删除@response
。我该怎么做?
或者我在这里太过分了,让事情变得过于复杂了?
I have a method in app/helpers/application_helper.rb
that calls cookies
. Which is totally allowed; this method is working wonderfully in my dev environment and knows what I mean when I say cookies
.
I am testing this helper with TestUnit (in test/unit/helpers/application_helper_test.rb
) and getting the error
NoMethodError: undefined method `cookies' for nil:NilClass
Even for four tests that do not set cookies. This has nothing to do with needing to 'set cookies in the test using cookie_jar
', because the issue isn't that I'm setting cookies and my helper isn't recognizing them, the issue is that the object cookies
is being called on is nil.
I wasn't sure which controller object cookies
was being called on, so I p
'd them all and found that, out of @controller
, @request
, and @response
, only @response
is nil. So I tried setting it to "cow". And indeed! My error changes:
NoMethodError: undefined method `cookies' for "cow":String
So it seems like I just need to stub out @response
. How do I do that?
Or am I way out of line here and overcomplicating things?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将此设置添加到执行此方法的所有测试中:
Add this setup to all of your tests that exercise this method: