Skip to content

Instantly share code, notes, and snippets.

@odlp
Last active January 2, 2019 10:58
Show Gist options
  • Save odlp/dd7f9806a29df7255aae0f85b6ad4e94 to your computer and use it in GitHub Desktop.
Save odlp/dd7f9806a29df7255aae0f85b6ad4e94 to your computer and use it in GitHub Desktop.
Conditional Bullet in RSpec tests
# 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