RSpec::Matchers.define :perform_queries do |expected|
  match do |block|
    query_count(&block) == expected
  end

  failure_message do |actual|
    "Expected to run #{expected} queries, got #{@counter.query_count}"
  end

  def query_count(&block)
    @counter = ActiveRecord::QueryCounter.new
    ActiveSupport::Notifications.subscribe('sql.active_record', @counter.to_proc)
    yield
    ActiveSupport::Notifications.unsubscribe(@counter.to_proc)

    @counter.query_count
  end

  def supports_block_expectations?
    true
  end
end

RSpec::Matchers.define :perform_queries do |expected|
  supports_block_expectations

  match do |block|
    query_count(&block) == expected
  end

  failure_message do |actual|
    "Expected to run #{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

Simplify matcher by using RSpec3 syntax
Specs: Simplify matcher by using RSpec3 syntax
