Skip to content

Instantly share code, notes, and snippets.

@charly-vega
Forked from kerrizor/bullet_and_minitest.md
Created April 24, 2017 02:45
Show Gist options
  • Save charly-vega/6353c88d7666224a0879931ee56f938c to your computer and use it in GitHub Desktop.
Save charly-vega/6353c88d7666224a0879931ee56f938c to your computer and use it in GitHub Desktop.
Trigger MiniTest failures in Rails when Bullet detects N+1 query violations

In test/test_helper.rb...

### Bullet (N+1 queries)

if ENV['BULLET']
  Bullet.enable = true

  require 'minitest/unit'

  module MiniTestWithBullet
    def before_setup
      Bullet.start_request
      super if defined?(super)
    end

    def after_teardown
      super if defined?(super)

      if Bullet.warnings.present?
        warnings = Bullet.warnings.map{ |_k, warning| warning }.flatten.map{|warn| warn.body_with_caller}.join("\n-----\n\n")

        flunk(warnings)
      end

      Bullet.end_request
    end
  end

  class ActiveSupport::TestCase
    include MiniTestWithBullet
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment