Class: RSpec::Mocks::MessageExpectation

Inherits:
BaseExpectation show all
Defined in:
lib/rspec/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, #matches?

Constructor Details

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

Instance Method Details

#actual_received_count_matters?Boolean

Returns:

  • (Boolean)


317
318
319
# File 'lib/rspec/mocks/message_expectation.rb', line 317

def actual_received_count_matters?
  @at_least || @at_most || @exactly
end

#advise(*args) ⇒ Object



242
243
244
# File 'lib/rspec/mocks/message_expectation.rb', line 242

def advise(*args)
  similar_messages << args
end

#any_number_of_times(&block) ⇒ Object



283
284
285
286
287
# File 'lib/rspec/mocks/message_expectation.rb', line 283

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

#at_least(n, &block) ⇒ Object



266
267
268
269
270
# File 'lib/rspec/mocks/message_expectation.rb', line 266

def at_least(n, &block)
  @method_block = block if block
  set_expected_received_count :at_least, n
  self
end

#at_most(n, &block) ⇒ Object



272
273
274
275
276
# File 'lib/rspec/mocks/message_expectation.rb', line 272

def at_most(n, &block)
  @method_block = block if block
  set_expected_received_count :at_most, n
  self
end

#exactly(n, &block) ⇒ Object



260
261
262
263
264
# File 'lib/rspec/mocks/message_expectation.rb', line 260

def exactly(n, &block)
  @method_block = block if block
  set_expected_received_count :exactly, n
  self
end

#expected_messages_received?Boolean

Returns:

  • (Boolean)


217
218
219
220
# File 'lib/rspec/mocks/message_expectation.rb', line 217

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

#generate_errorObject



246
247
248
249
250
251
252
# File 'lib/rspec/mocks/message_expectation.rb', line 246

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_similar_message_args_error(self, *@similar_messages)
  end
end

#ignoring_args?Boolean

Returns:

  • (Boolean)


222
223
224
# File 'lib/rspec/mocks/message_expectation.rb', line 222

def ignoring_args?
  @expected_received_count == :any
end

#increase_actual_received_count!Object



321
322
323
# File 'lib/rspec/mocks/message_expectation.rb', line 321

def increase_actual_received_count!
  @actual_received_count += 1
end

#matches_at_least_count?Boolean

Returns:

  • (Boolean)


226
227
228
# File 'lib/rspec/mocks/message_expectation.rb', line 226

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

#matches_at_most_count?Boolean

Returns:

  • (Boolean)


230
231
232
# File 'lib/rspec/mocks/message_expectation.rb', line 230

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

#matches_exact_count?Boolean

Returns:

  • (Boolean)


234
235
236
# File 'lib/rspec/mocks/message_expectation.rb', line 234

def matches_exact_count?
  @expected_received_count == @actual_received_count
end

#matches_name_but_not_args(sym, *args) ⇒ Object



204
205
206
# File 'lib/rspec/mocks/message_expectation.rb', line 204

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

#negative_expectation_for?(sym) ⇒ Boolean

Returns:

  • (Boolean)


313
314
315
# File 'lib/rspec/mocks/message_expectation.rb', line 313

def negative_expectation_for?(sym)
  return false
end

#neverObject



289
290
291
292
# File 'lib/rspec/mocks/message_expectation.rb', line 289

def never
  @expected_received_count = 0
  self
end

#once(&block) ⇒ Object



294
295
296
297
298
# File 'lib/rspec/mocks/message_expectation.rb', line 294

def once(&block)
  @method_block = block if block
  set_expected_received_count :exactly, 1
  self
end

#ordered(&block) ⇒ Object



306
307
308
309
310
311
# File 'lib/rspec/mocks/message_expectation.rb', line 306

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

#similar_messagesObject



238
239
240
# File 'lib/rspec/mocks/message_expectation.rb', line 238

def similar_messages
  @similar_messages ||= []
end

#times(&block) ⇒ Object



278
279
280
281
# File 'lib/rspec/mocks/message_expectation.rb', line 278

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

#twice(&block) ⇒ Object



300
301
302
303
304
# File 'lib/rspec/mocks/message_expectation.rb', line 300

def twice(&block)
  @method_block = block if block
  set_expected_received_count :exactly, 2
  self
end

#verify_messages_receivedObject



208
209
210
211
212
213
214
215
# File 'lib/rspec/mocks/message_expectation.rb', line 208

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

#with(*args, &block) ⇒ Object



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

def with(*args, &block)
  @return_block = block if block_given? unless args.empty?
  @args_expectation = ArgumentExpectation.new(*args, &block)
  self
end