Class: RSpec::ActiveRecord::Expectations::Matchers::QueryCountMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/activerecord/expectations/matchers/query_count_matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQueryCountMatcher

Returns a new instance of QueryCountMatcher.



6
7
8
9
10
11
12
13
# File 'lib/rspec/activerecord/expectations/matchers/query_count_matcher.rb', line 6

def initialize
  @collector = Collector.new
  @message_builder = MessageBuilder.new(self)

  @match_method = nil
  @quantifier   = nil
  @query_type   = nil
end

Instance Attribute Details

#collectorObject (readonly)

Returns the value of attribute collector.



4
5
6
# File 'lib/rspec/activerecord/expectations/matchers/query_count_matcher.rb', line 4

def collector
  @collector
end

#comparisonObject (readonly)

Returns the value of attribute comparison.



4
5
6
# File 'lib/rspec/activerecord/expectations/matchers/query_count_matcher.rb', line 4

def comparison
  @comparison
end

#quantifierObject (readonly)

Returns the value of attribute quantifier.



4
5
6
# File 'lib/rspec/activerecord/expectations/matchers/query_count_matcher.rb', line 4

def quantifier
  @quantifier
end

#query_typeObject (readonly)

Returns the value of attribute query_type.



4
5
6
# File 'lib/rspec/activerecord/expectations/matchers/query_count_matcher.rb', line 4

def query_type
  @query_type
end

Instance Method Details

#actual_countObject

helper for message builder



98
99
100
# File 'lib/rspec/activerecord/expectations/matchers/query_count_matcher.rb', line 98

def actual_count
  @collector.queries_of_type(@query_type)
end

#exactly(n) ⇒ Object



72
73
74
75
76
77
# File 'lib/rspec/activerecord/expectations/matchers/query_count_matcher.rb', line 72

def exactly(n)
  @quantifier   = n
  @comparison   = :exactly
  @match_method = -> { actual_count == @quantifier }
  self
end

#failure_messageObject



15
16
17
# File 'lib/rspec/activerecord/expectations/matchers/query_count_matcher.rb', line 15

def failure_message
  @message_builder.failure_message
end

#failure_message_when_negatedObject



19
20
21
# File 'lib/rspec/activerecord/expectations/matchers/query_count_matcher.rb', line 19

def failure_message_when_negated
  @message_builder.failure_message_when_negated
end

#greater_than(n) ⇒ Object Also known as: more_than



56
57
58
59
60
61
# File 'lib/rspec/activerecord/expectations/matchers/query_count_matcher.rb', line 56

def greater_than(n)
  @quantifier   = n
  @comparison   = :greater_than
  @match_method = -> { actual_count > @quantifier }
  self
end

#greater_than_or_equal_to(n) ⇒ Object Also known as: at_least



64
65
66
67
68
69
# File 'lib/rspec/activerecord/expectations/matchers/query_count_matcher.rb', line 64

def greater_than_or_equal_to(n)
  @quantifier   = n
  @comparison   = :greater_than_or_equal_to
  @match_method = -> { actual_count >= @quantifier }
  self
end

#less_than(n) ⇒ Object Also known as: fewer_than

QUANTIFIERS



40
41
42
43
44
45
# File 'lib/rspec/activerecord/expectations/matchers/query_count_matcher.rb', line 40

def less_than(n)
  @quantifier   = n
  @comparison   = :less_than
  @match_method = -> { actual_count < @quantifier }
  self
end

#less_than_or_equal_to(n) ⇒ Object Also known as: at_most



48
49
50
51
52
53
# File 'lib/rspec/activerecord/expectations/matchers/query_count_matcher.rb', line 48

def less_than_or_equal_to(n)
  @quantifier   = n
  @comparison   = :less_than_or_equal_to
  @match_method = -> { actual_count <= @quantifier }
  self
end

#matches?(block) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



27
28
29
30
31
32
33
34
35
36
# File 'lib/rspec/activerecord/expectations/matchers/query_count_matcher.rb', line 27

def matches?(block)
  raise NoComparisonError unless @match_method
  raise NoQueryTypeError unless @collector.valid_type?(@query_type)

  block.call
  result = @match_method.call
  @collector.finalize

  result
end

#supports_block_expectations?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/rspec/activerecord/expectations/matchers/query_count_matcher.rb', line 23

def supports_block_expectations?
  true
end