Skip to content
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

Conditionally turn off audits in tests #699

Open
JerrodCarpenter opened this issue Feb 26, 2024 · 1 comment
Open

Conditionally turn off audits in tests #699

JerrodCarpenter opened this issue Feb 26, 2024 · 1 comment

Comments

@JerrodCarpenter
Copy link

I'm trying to find a way to only have audits on for the specific tests that test audits. Turning audits off for the 95% of other tests will really speed up test suites. I can't quite figure out how to turn this on/off on a file per file or test by test basis though. Has anyone done this in the past?

@mattr
Copy link

mattr commented Nov 27, 2024

If you're using RSpec, you can set your defaults in the rails helper and then override the settings per test:

# rails_helper.rb
config.before(:all) { Audited.auditing_enabled = false }
config.after(:all) { Audited.auditing_enabled = true } # opt: ensure audits are re-enabled after run

and in your individual tests where you need audits:

around do |example|
  Audited.auditing_enabled = true
  example.run
  Audited.auditing_enabled = false
end

It should be possible to do something similar with minitest. A quick search suggests that it doesn't have a before(:all) equivalent, but you could set Audited.auditing_enabled = false in your test environment/config, and then use setup and teardown to enable/disable for individual tests. Minitest does have an after_tests hook which you could use to ensure that auditing is re-enabled after the test suite has run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants