Recently I’ve been refactoring the tests for a gem I maintain and I needed to test that it sets the right cookies at the right time. But the cookie...
For further actions, you may consider blocking this person and/or reporting abuse
Nice article and great tips. I would suggest switching the order of the "asserts" as the first argument is the "expected" value.
I'm using
ActionDispatch::IntegrationTest( minitest) and I found that if I usecookiesinstead ofresponse.cookiesthen it returns the "request cookies". Have you encountered the same behaviour.Ah, you're right. I've switched those asserts around.
I am mostly writing my tests in RSpec, with RSpec-Rails, at the moment. Their documentation recommends using
cookiesoverresponse.cookiesand that's worked for me so far.While writing this up, it was confusing what the difference between
cookiesandresponse.cookiesis within the minitest versions. I might have to dig into that further to work out exactly what is going on.Alright! I'm currently in the process of refactoring some tests using minitest (with Rails), will let you know what would potentially work for me (hopefully).
I've spelunked the rails code source,
#cookiesin minitest always refers to the request's cookies ( even after the HTTP call), and is aRack::Test::CookieJarinstance, whereasresponse.cookiesis a plain ruby hash containing the responses's cookies. I'm going to instantiate a rails Cookie Jar as suggested in your article, but usingresponse.cookiesas the second argument.Update: I've figured out how to make it work in my tests ( was having weird issues but application related, not rails related)
Basically to make this work in Rails/minitest, one would do:
We would need to use
response.cookies. No need to add.to_hashbecause response.cookies is already a hash.Unlike Rspec,
cookiesin Minitest ( or Rails custom version of minitest) always refer to the request cookies which is an instance ofRack::Test::CookieJarThis is the answer I have been looking for. I had originally used
But realised late on that the cookies.to_hash was empty as I never set cookies in Rack::Test::CookieJar
Thank you for this