Last active
January 2, 2019 10:58
-
-
Save odlp/dd7f9806a29df7255aae0f85b6ad4e94 to your computer and use it in GitHub Desktop.
Conditional Bullet in RSpec tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Make Bullet failing tests which opt-in to 'strict' mode | |
# | |
# Based on: | |
# https://github.com/flyerhzm/bullet#run-in-tests | |
# spec/rails_helper.rb or similar | |
RSpec.configure do |config| | |
config.before(:example, :ar_strict) do | |
Bullet.enable = true | |
Bullet.bullet_logger = true | |
Bullet.raise = true # raise an error if n+1 query occurs | |
Bullet.start_request | |
end | |
config.after(:example, :ar_strict) do | |
Bullet.perform_out_of_channel_notifications if Bullet.notification? | |
Bullet.end_request | |
ensure | |
Bullet.enable = false | |
Bullet.bullet_logger = false | |
Bullet.raise = false | |
end | |
end | |
# spec/features/feature_spec.rb | |
require "rails_helper" | |
RSpec.describe "enforcing good AR use" do | |
it "fails any N+1 queries", :ar_strict do | |
# No N+1's allowed | |
end | |
it "allows any N+1 queries" do | |
# N+1 away | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment