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_call_original, #and_raise, #and_return, #and_yield, #with

Constructor Details

#initialize(target, method_name) ⇒ ExpectationChain

Returns a new instance of ExpectationChain.



163
164
165
166
167
168
169
# File 'lib/crspec/mock/double.rb', line 163

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



189
190
191
192
193
# File 'lib/crspec/mock/double.rb', line 189

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

#exactly(n) ⇒ Object



183
184
185
186
187
# File 'lib/crspec/mock/double.rb', line 183

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

#onceObject



171
172
173
174
175
# File 'lib/crspec/mock/double.rb', line 171

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

#twiceObject



177
178
179
180
181
# File 'lib/crspec/mock/double.rb', line 177

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

#verify_expectations!Object

Raises:



195
196
197
198
199
200
201
202
203
204
205
# File 'lib/crspec/mock/double.rb', line 195

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