diff options
Diffstat (limited to 'spec/support/matchers/make_queries.rb')
-rw-r--r-- | spec/support/matchers/make_queries.rb | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/spec/support/matchers/make_queries.rb b/spec/support/matchers/make_queries.rb new file mode 100644 index 00000000000..19c69240a40 --- /dev/null +++ b/spec/support/matchers/make_queries.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +RSpec::Matchers.define :make_queries do |expected_count = nil| + supports_block_expectations + + match do |block| + @recorder = ActiveRecord::QueryRecorder.new(&block) + @counter = @recorder.count + if expected_count + @counter == expected_count + else + @counter > 0 + end + end + + failure_message do |_| + if expected_count + "expected to make #{expected_count} queries but made #{@counter} queries" + else + "expected to make queries but did not make any" + end + end + + failure_message_when_negated do |_| + if expected_count + "expected not to make #{expected_count} queries but received #{@counter} queries" + else + "expected not to make queries but received #{@counter} queries" + end + end +end |