debian-mirror-gitlab/spec/support/matchers/make_queries.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

32 lines
779 B
Ruby
Raw Permalink Normal View History

2022-07-16 23:28:13 +05:30
# 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