
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 usecookies
instead ofresponse.cookies
then 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
cookies
overresponse.cookies
and that's worked for me so far.While writing this up, it was confusing what the difference between
cookies
andresponse.cookies
is within the minitest versions. I might have to dig into that further to work out exactly what is going on.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_hash
because response.cookies is already a hash.Unlike Rspec,
cookies
in Minitest ( or Rails custom version of minitest) always refer to the request cookies which is an instance ofRack::Test::CookieJar
This 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
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,
#cookies
in minitest always refers to the request's cookies ( even after the HTTP call), and is aRack::Test::CookieJar
instance, whereasresponse.cookies
is 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.cookies
as the second argument.