Open mejibyte opened 6 years ago
Glad you tracked down the issue. This sounds like something RSpec would handle than the test helpers themselves. The test helpers are test framework agnostic and thus have no concept when a test starts, ends, or moves onto the next one.
What I'd suggest adding is a shared context in RSpec to cleanup authenticated requests:
RSpec.shared_context "authenticated", authenticated: true do
after(:each) { Warden.test_reset! }
end
RSpec.configure do |config|
config.include_context "authenticated", authenticated: true
end
Then on any test that needs authentication you can add it 'logs someone in', authenticated: true do ... end
.
If most of your tests require authentication, you can follow the devise recommendation and setup a global after(:each) hook:
RSpec.configure do |config|
config.after(:each) { Warden.test_reset! }
end
Hope this helps
I have an RSpec test in a Rails app that calls
login_as
and looks something like this:It passes:
Accidentally, I added a second
login_as
in test A (and didn't notice -- in real life my test is more complicated so it was easy to miss):This resulted in flaky tests that would fail or pass depending on the order in which RSpec chose to run them (it works if B runs before A).
After hours of debugging, I realized the problem is that the second
login_as
call in test A is "leaking" to test B. Looking at the output, you can see that the Warden user key in the session printed from test B is the same as the one printed from test A!This is very surprising and breaks test isolation in an unexpected way. Can we make the "queue" of pending
login_as
calls automatically disappear at the end of each spec? (Not sure if this is even feasible.)