Welcome to mirror list, hosted at ThFree Co, Russian Federation.

make_queries.rb « matchers « support « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 19c69240a406538b8e651b52610cead9698427c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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