Class: Crspec::Mock::ExpectationChain

Inherits:
StubChain
  • Object
show all
Defined in:
lib/crspec/mock/double.rb

Instance Attribute Summary

Attributes inherited from StubChain

#method_name, #target

Instance Method Summary collapse

Methods inherited from StubChain

#and_raise, #and_return, #and_yield, #with

Constructor Details

#initialize(target, method_name) ⇒ ExpectationChain

Returns a new instance of ExpectationChain.



106
107
108
109
110
111
112
# File 'lib/crspec/mock/double.rb', line 106

def initialize(target, method_name)
  @call_count = 0
  @expected_count = 1
  @count_constraint = :exact
  super(target, method_name)
  Space.current.register_expectation(self)
end

Instance Method Details

#at_least(n) ⇒ Object



132
133
134
135
136
# File 'lib/crspec/mock/double.rb', line 132

def at_least(n)
  @expected_count = n
  @count_constraint = :at_least
  self
end

#exactly(n) ⇒ Object



126
127
128
129
130
# File 'lib/crspec/mock/double.rb', line 126

def exactly(n)
  @expected_count = n
  @count_constraint = :exact
  self
end

#onceObject



114
115
116
117
118
# File 'lib/crspec/mock/double.rb', line 114

def once
  @expected_count = 1
  @count_constraint = :exact
  self
end

#twiceObject



120
121
122
123
124
# File 'lib/crspec/mock/double.rb', line 120

def twice
  @expected_count = 2
  @count_constraint = :exact
  self
end

#verify_expectations!Object

Raises:



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/crspec/mock/double.rb', line 138

def verify_expectations!
  valid = case @count_constraint
          when :exact then @call_count == @expected_count
          when :at_least then @call_count >= @expected_count
          end

  return if valid

  raise MockError,
        "Expected #{@target.inspect} to receive #{@method_name.inspect} #{@count_constraint} #{@expected_count} times, but received it #{@call_count} times"
end