Controller integration tests lose the session after follow_redirect

64 Views Asked by At

When testing a rails 7 controller everything works splendidly until I get a redirect and then execute a follow_redirect!. At this point the session mysteriously disappears and my tests can no longer continue...very limiting.

Very dumbed down, hypothetical example:

post some_url, params { yada: 'foo' }
assert_not_nil session[:yada]
assert_redirect some_redirect_url
follow_redirect!
assert_not_nil session[:yada] # This assertion fails, the session is gone
1

There are 1 best solutions below

0
On

TLDR: Check to see if you're using :null_store for your config.cache_store setting in tests. If so switch the config line to: config.cache_store = :memory_store

After MUCH debugging and consternation, I was finally able to track down the issue. In the test.rb rails configuration file the config.cache_store setting was set to :null_store. Swapping this out for :memory_store fixed the issue and all was well again. Worked like a charm...but took several hours to finally track down.