-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RSpec does not reset ActiveSupport::CurrentAttributes around each example #2503
Comments
As per Rails doc:
The reset code is injected by Rails here into In On the one hand, it makes total sense to reset the current attributes. On the other - it is reset before and after each request, and why would we explicitly reset it in test support code once again? app.executor.to_run { ActiveSupport::CurrentAttributes.reset_all }
app.executor.to_complete { ActiveSupport::CurrentAttributes.reset_all } Can you please check if the issue still reproduces for controller/feature/request specs? |
Thanks for the quick response! Yes, I think your assessment is correct. I just confirmed that request specs do not have this problem. As you said, it is only an issue for "plain tests". My understanding is that request specs, since they simulate actual web requests, are inherently using the app "executor". That executor takes care of resetting the current attributes. In this case the test environment is very similar to the actual production environment, and so the behavior of current attributes is similar. For a "plain test" like a model spec, there is no request/response or active job execution being simulated. Hence the app executor is not involved, and so current attributes will not be reset via the executor. That is why rails/rails#39493 introduced the explicit reset in I think this is reasonable behavior and expected for a framework feature like |
Agree. I see your point and the reasoning behind rails/rails#39493 now as well. What I mean by this is e.g. What I could quickly find in the wild: # chatwoot/app/models/user.rb
class User < ApplicationRecord
def current_account_user
account_users.find_by(account_id: Current.account.id) if Current.account
end
...
def assigned_inboxes
inboxes.where(account_id: Current.account.id)
end So, apparently, an example group with no explicit setup could rely on the fact that Personally, I feel a smell when I have no objections to add the reset code to all Rails-specific example groups ( |
If you add |
Ah, you're right! I had originally encountered this problem with current attributes in a rails app where I have I see this is a mistake on my part; I need to configure rspec to recognize those directories. Something like this, I guess? RSpec.configure do |config|
config.include(
RSpec::Rails::RailsExampleGroup,
file_path: %r{spec/commands|spec/services}
)
end In any case my original issue does not seem to be a bug, so I will close. |
This recreates rspec#2712 since the GHA logs are no longer available which we need to address this issue. We should be wrapping the Rails examples with the executor to mimic what Rails v7 does (as noted in rspec#2713) to properly reset state. This has been tested in a test suite for an app but we need to address what issues there is within rspec-rails. This change request originated from rspec#2503 and rspec#2752 which the latter is meant as a temporary fix since this is the expected way to reset state.
To help others finding this issue, I was confused for awhile as to why this was closed. I realize it has to do with new Rails versions since. This was for Rails 6.1 so likely using Since rspec-rails already includes the execution context test helper, I've created #2752 with a temporary fix to include the current attributes test helper to match what Rails 7 does. That will still require a rails based spec (e.g. |
This recreates rspec#2712 since the GHA logs are no longer available which we need to address this issue. We should be wrapping the Rails examples with the executor to mimic what Rails v7 does (as noted in rspec#2713) to properly reset state. This has been tested in a test suite for an app but we need to address what issues there is within rspec-rails. This change request originated from rspec#2503 and rspec#2752 which the latter is meant as a temporary fix since this is the expected way to reset state.
What Ruby, Rails and RSpec versions are you using?
Ruby version: ruby 3.0.1p64 (2021-04-05 revision 0fb782ee38) [x86_64-darwin20]
Rails version: 6.1.3.2
RSpec version: RSpec 3.10
Observed behaviour
rspec-rails does not reset
CurrentAttributes
. That means if one test sets a value usingCurrentAttributes
, it can very easily pollute other tests.Expected behaviour
Rails
ActiveSupport::TestCase
automatically resetsCurrentAttributes
between each test (see active_support/current_attributes/test_helper.rb). I would expect rspec-rails to do the same.Can you provide an example app?
Here is a trivial example app using
CurrentAttributes
and anActiveSupport::TestCase
that passes:Test output:
However the same app, when tested via rspec-rails, fails:
Spec output:
Here is the Gemfile I used:
The text was updated successfully, but these errors were encountered: