Class: RSpec::ActiveRecord::Expectations::Matchers::TransactionMatcher

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transaction_type) ⇒ TransactionMatcher

Returns a new instance of TransactionMatcher.



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

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

  self.at_least(1)
end

Instance Attribute Details

#collectorObject (readonly)

Returns the value of attribute collector.



4
5
6
# File 'lib/rspec/activerecord/expectations/matchers/transaction_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/transaction_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/transaction_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/transaction_matcher.rb', line 4

def query_type
  @query_type
end

Instance Method Details

#actual_countObject



94
95
96
# File 'lib/rspec/activerecord/expectations/matchers/transaction_matcher.rb', line 94

def actual_count
  @collector.queries_of_type(@query_type)
end

#exactly(n) ⇒ Object



70
71
72
73
74
75
# File 'lib/rspec/activerecord/expectations/matchers/transaction_matcher.rb', line 70

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

#failure_messageObject



14
15
16
# File 'lib/rspec/activerecord/expectations/matchers/transaction_matcher.rb', line 14

def failure_message
  @message_builder.failure_message
end

#failure_message_when_negatedObject



18
19
20
# File 'lib/rspec/activerecord/expectations/matchers/transaction_matcher.rb', line 18

def failure_message_when_negated
  @message_builder.failure_message_when_negated
end

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



54
55
56
57
58
59
# File 'lib/rspec/activerecord/expectations/matchers/transaction_matcher.rb', line 54

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



62
63
64
65
66
67
# File 'lib/rspec/activerecord/expectations/matchers/transaction_matcher.rb', line 62

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



38
39
40
41
42
43
# File 'lib/rspec/activerecord/expectations/matchers/transaction_matcher.rb', line 38

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



46
47
48
49
50
51
# File 'lib/rspec/activerecord/expectations/matchers/transaction_matcher.rb', line 46

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:



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

def matches?(block)
  raise NoComparisonError unless @match_method

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

  result
end

#onceObject



77
78
79
# File 'lib/rspec/activerecord/expectations/matchers/transaction_matcher.rb', line 77

def once
  exactly(1).time
end

#supports_block_expectations?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/rspec/activerecord/expectations/matchers/transaction_matcher.rb', line 22

def supports_block_expectations?
  true
end

#thriceObject

hehe



85
86
87
# File 'lib/rspec/activerecord/expectations/matchers/transaction_matcher.rb', line 85

def thrice # hehe
  exactly(3).times
end

#timesObject Also known as: time



89
90
91
# File 'lib/rspec/activerecord/expectations/matchers/transaction_matcher.rb', line 89

def times
  self # NOOP
end

#twiceObject



81
82
83
# File 'lib/rspec/activerecord/expectations/matchers/transaction_matcher.rb', line 81

def twice
  exactly(2).times
end