# https://gist.github.com/rsutphin/af06c9e3dadf658d2293 # Derived from http://stackoverflow.com/a/13423584/153896. Updated for RSpec 3. RSpec::Matchers.define :under_query_limit do |expected| supports_block_expectations match do |block| query_count(&block) <= expected end failure_message do |actual| "Expected to run maximum #{expected} queries, got #{@counter.query_count}" end def query_count(&block) @counter = ActiveRecord::QueryCounter.new ActiveSupport::Notifications.subscribed(@counter.to_proc, 'sql.active_record', &block) @counter.query_count end end