steveklabnik / request_store

Per-request global storage for Rack.
https://github.com/steveklabnik/request_store
MIT License
1.48k stars 87 forks source link

RequestStore not cleared in RSpec non-controller tests #75

Open key88sf opened 3 years ago

key88sf commented 3 years ago

Rails 5, Rspec 3.10.0

It looks like RequestStore is not being cleared in-between rspec unit tests when used in something like a helper spec, or any non-controller unit tests.

Do we need to manually clear it for these, or should this be automatic in any Rails rspec test?

KelseyDH commented 3 years ago

Ideally we wouldn't be doing this for request specs, but you could hack around this problem with rspec by adding to your spec_helper.rb:

  config.before do
    RequestStore.clear!
  end
key88sf commented 3 years ago

@KelseyDH Yeah that is what we ended up doing for now.

allantokuda-zipnosis commented 1 year ago

I had a similar issue, and found @KelseyDH's suggestion did not solve my problem because my Rspec tests were expecting data changes during the test, and RequestStore was dutifully making the test code act as if it were all in one request, so I could not see the data change. As a solution, I found this mock useful to allow a specific test to bypass the effects of a RequestStore.fetch. NOT recommended for controller or request specs!

allow(RequestStore).to receive(:fetch) { |&block| block.call }