Class: Spec::Mocks::MessageExpectation

Inherits:
BaseExpectation show all
Defined in:
lib/spec/mocks/message_expectation.rb

Direct Known Subclasses

NegativeMessageExpectation

Instance Attribute Summary

Attributes inherited from BaseExpectation

#sym

Instance Method Summary collapse

Methods inherited from BaseExpectation

#and_raise, #and_return, #and_throw, #and_yield, #build_child, #called_max_times?, #expected_args, #initialize, #invoke, #invoke_return_block, #matches

Constructor Details

This class inherits a constructor from Spec::Mocks::BaseExpectation

Instance Method Details

#advise(args, block) ⇒ Object



227
228
229
# File 'lib/spec/mocks/message_expectation.rb', line 227

def advise(args, block)
  similar_messages << args
end

#any_number_of_times(&block) ⇒ Object



264
265
266
267
268
# File 'lib/spec/mocks/message_expectation.rb', line 264

def any_number_of_times(&block)
  @method_block = block if block
  @expected_received_count = :any
  self
end

#at_least(n) ⇒ Object



249
250
251
252
# File 'lib/spec/mocks/message_expectation.rb', line 249

def at_least(n)
  set_expected_received_count :at_least, n
  self
end

#at_most(n) ⇒ Object



254
255
256
257
# File 'lib/spec/mocks/message_expectation.rb', line 254

def at_most(n)
  set_expected_received_count :at_most, n
  self
end

#exactly(n) ⇒ Object



244
245
246
247
# File 'lib/spec/mocks/message_expectation.rb', line 244

def exactly(n)
  set_expected_received_count :exactly, n
  self
end

#expected_messages_received?Boolean

Returns:

  • (Boolean)


202
203
204
205
# File 'lib/spec/mocks/message_expectation.rb', line 202

def expected_messages_received?
  ignoring_args? || matches_exact_count? ||
     matches_at_least_count? || matches_at_most_count?
end

#generate_errorObject



231
232
233
234
235
236
237
# File 'lib/spec/mocks/message_expectation.rb', line 231

def generate_error
  if similar_messages.empty?
    @error_generator.raise_expectation_error(@sym, @expected_received_count, @actual_received_count, *@args_expectation.args)
  else
    @error_generator.raise_unexpected_message_args_error(self, *@similar_messages)
  end
end

#ignoring_args?Boolean

Returns:

  • (Boolean)


207
208
209
# File 'lib/spec/mocks/message_expectation.rb', line 207

def ignoring_args?
  @expected_received_count == :any
end

#matches_at_least_count?Boolean

Returns:

  • (Boolean)


211
212
213
# File 'lib/spec/mocks/message_expectation.rb', line 211

def matches_at_least_count?
  @at_least && @actual_received_count >= @expected_received_count
end

#matches_at_most_count?Boolean

Returns:

  • (Boolean)


215
216
217
# File 'lib/spec/mocks/message_expectation.rb', line 215

def matches_at_most_count?
  @at_most && @actual_received_count <= @expected_received_count
end

#matches_exact_count?Boolean

Returns:

  • (Boolean)


219
220
221
# File 'lib/spec/mocks/message_expectation.rb', line 219

def matches_exact_count?
  @expected_received_count == @actual_received_count
end

#matches_name?(sym) ⇒ Boolean

Returns:

  • (Boolean)


185
186
187
# File 'lib/spec/mocks/message_expectation.rb', line 185

def matches_name?(sym)
  @sym == sym
end

#matches_name_but_not_args(sym, args) ⇒ Object



189
190
191
# File 'lib/spec/mocks/message_expectation.rb', line 189

def matches_name_but_not_args(sym, args)
  matches_name?(sym) and not @args_expectation.args_match?(args)
end

#negative_expectation_for?(sym) ⇒ Boolean

Returns:

  • (Boolean)


294
295
296
# File 'lib/spec/mocks/message_expectation.rb', line 294

def negative_expectation_for?(sym)
  return false
end

#neverObject



270
271
272
273
# File 'lib/spec/mocks/message_expectation.rb', line 270

def never
  @expected_received_count = 0
  self
end

#once(&block) ⇒ Object



275
276
277
278
279
# File 'lib/spec/mocks/message_expectation.rb', line 275

def once(&block)
  @method_block = block if block
  @expected_received_count = 1
  self
end

#ordered(&block) ⇒ Object



287
288
289
290
291
292
# File 'lib/spec/mocks/message_expectation.rb', line 287

def ordered(&block)
  @method_block = block if block
  @order_group.register(self)
  @ordered = true
  self
end

#similar_messagesObject



223
224
225
# File 'lib/spec/mocks/message_expectation.rb', line 223

def similar_messages
  @similar_messages ||= []
end

#times(&block) ⇒ Object



259
260
261
262
# File 'lib/spec/mocks/message_expectation.rb', line 259

def times(&block)
  @method_block = block if block
  self
end

#twice(&block) ⇒ Object



281
282
283
284
285
# File 'lib/spec/mocks/message_expectation.rb', line 281

def twice(&block)
  @method_block = block if block
  @expected_received_count = 2
  self
end

#verify_messages_receivedObject



193
194
195
196
197
198
199
200
# File 'lib/spec/mocks/message_expectation.rb', line 193

def verify_messages_received
  return if expected_messages_received? || failed_fast?
    
  generate_error
rescue Spec::Mocks::MockExpectationError => error
  error.backtrace.insert(0, @expected_from)
  Kernel::raise error
end

#with(*args, &block) ⇒ Object



239
240
241
242
# File 'lib/spec/mocks/message_expectation.rb', line 239

def with(*args, &block)
  @args_expectation = ArgumentExpectation.new(args, &block)
  self
end