testing a rails helper that uses cookies
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?
Add this setup to all of your tests that exercise this method:
@response.stubs(:cookies).returns({})
精彩评论