简体   繁体   English

测试使用Cookie的Rails助手

[英]testing a rails helper that uses cookies

I have a method in app/helpers/application_helper.rb that calls cookies . 我在app/helpers/application_helper.rb中有一个调用cookies Which is totally allowed; 完全允许; this method is working wonderfully in my dev environment and knows what I mean when I say cookies . 这种方法在我的开发环境中效果很好,并且知道我说cookies意思。

I am testing this helper with TestUnit (in test/unit/helpers/application_helper_test.rb ) and getting the error 我正在用TestUnit(在test/unit/helpers/application_helper_test.rb )测试此助手,并得到错误

NoMethodError: undefined method `cookies' for nil:NilClass

Even for four tests that do not set cookies . 即使对于四个未设置cookie的测试。 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 . 这与需要“使用cookie_jar在测试中设置cookie” cookie_jar ,因为问题不在于我正在设置cookie,而我的助手却无法识别它们,问题在于正在调用对象 cookies

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. 我不知道该控制器对象cookies被调用,所以我p倒是他们都发现,出于@controller@request ,并@response ,只有@response是零。 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 . 所以似乎我只需要存根@response How do I do that? 我怎么做?

Or am I way out of line here and overcomplicating things? 还是我在这里让事情变得异常复杂?

将此设置添加到使用此方法的所有测试中:

@response.stubs(:cookies).returns({})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM