Sha256: 91bccb23ac8d4d60db5f2a019af70de4aa59f5a9d96d255b18c54f037c667a6b

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

module Rspec
  module QueryLimit
    RSpec::Matchers.define :query_limit_eq do |expected|

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

      if self.respond_to?(:failure_message)
        failure_message do |actual|
          failure_text
        end

        failure_message_when_negated  do |actual|
          failure_text_negated
        end
      else
        failure_message_for_should do |actual|
          failure_text
        end

        failure_message_for_should_not do |actual|
          failure_text_negated
        end
      end

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

      def supports_block_expectations?
        true
      end

      def failure_text
        "Expected to run exactly #{expected} queries, got #{@counter.query_count}"
      end

      def failure_text_negated
        "Expected to run other than #{expected} queries, got #{@counter.query_count}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rspec-query-limit-0.1.1 lib/rspec/query_limit/matchers.rb