Class: RSpec::Mocks::MessageExpectation
- Inherits:
-
Object
- Object
- RSpec::Mocks::MessageExpectation
- Defined in:
- lib/rspec/mocks/message_expectation.rb
Instance Attribute Summary (collapse)
-
- (Object) argument_list_matcher
writeonly
Sets the attribute argumentlistmatcher.
-
- (Object) message
readonly
Returns the value of attribute message.
Instance Method Summary (collapse)
-
- (Object) and_call_original
Tells the object to delegate to the original unmodified method when it receives the message.
-
- (Object) and_raise(exception = RuntimeError, message = nil)
Tells the object to raise an exception when the message is received.
-
- (Object) and_return(*values)
Tells the object to return a value when it receives the message.
-
- (Object) and_throw(*args)
Tells the object to throw a symbol (with the object if that form is used) when the message is received.
-
- (Object) and_yield(*args) {|@eval_context = Object.new.extend(RSpec::Mocks::InstanceExec)| ... }
Tells the object to yield one or more args to a block when the message is received.
-
- (Object) any_number_of_times
Allows an expected message to be received any number of times.
-
- (Object) at_least(n)
Constrain a message expectation to be received at least a specific number of times.
-
- (Object) at_most(n)
Constrain a message expectation to be received at most a specific number of times.
-
- (Object) exactly(n)
Constrain a message expectation to be received a specific number of times.
- - (Object) expectation_count_type
-
- (Object) never
Expect a message not to be received at all.
-
- (Object) once
Expect a message to be received exactly one time.
-
- (Object) ordered
Expect messages to be received in a specific order.
- - (Object) raise_out_of_order_error
-
- (Object) times
Syntactic sugar for
exactly
,at_least
andat_most
. -
- (Object) twice
Expect a message to be received exactly two times.
-
- (Object) with(*args)
Constrains a stub or message expectation to invocations with specific arguments.
Instance Attribute Details
- (Object) argument_list_matcher=(value) (writeonly)
Sets the attribute argumentlistmatcher
8 9 10 |
# File 'lib/rspec/mocks/message_expectation.rb', line 8 def argument_list_matcher=(value) @argument_list_matcher = value end |
- (Object) message (readonly)
Returns the value of attribute message
7 8 9 |
# File 'lib/rspec/mocks/message_expectation.rb', line 7 def @message end |
Instance Method Details
- (Object) and_call_original
This is only available on partial mock objects.
Tells the object to delegate to the original unmodified method when it receives the message.
101 102 103 104 105 106 107 |
# File 'lib/rspec/mocks/message_expectation.rb', line 101 def and_call_original if @method_double.object.is_a?(RSpec::Mocks::TestDouble) @error_generator.raise_only_valid_on_a_partial_mock(:and_call_original) else @implementation = AndCallOriginalImplementation.new(@method_double.original_method) end end |
- (Object) and_raise - (Object) and_raise(ExceptionClass) - (Object) and_raise(ExceptionClass, message) - (Object) and_raise(exception_instance)
When you pass an exception class, the MessageExpectation will raise
an instance of it, creating it with exception
and passing message
if specified. If the exception class initializer requires more than
one parameters, you must pass in an instance and not the class,
otherwise this method will raise an ArgumentError exception.
Tells the object to raise an exception when the message is received.
130 131 132 133 134 135 136 137 |
# File 'lib/rspec/mocks/message_expectation.rb', line 130 def and_raise(exception = RuntimeError, = nil) if exception.respond_to?(:exception) exception = ? exception.exception() : exception.exception end self.terminal_implementation_action = Proc.new { raise exception } nil end |
- (Object) and_return(value) - (Object) and_return(first_value, second_value) - (Object) and_return(&block)
Tells the object to return a value when it receives the message. Given more than one value, the first value is returned the first time the message is received, the second value is returned the next time, etc, etc.
If the message is received more times than there are values, the last value is received for every subsequent call.
The block format is still supported, but is unofficially deprecated in favor of just passing a block to the stub method.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rspec/mocks/message_expectation.rb', line 73 def and_return(*values, &implementation) if negative? RSpec.deprecate "`and_return` on a negative message expectation" end @expected_received_count = [@expected_received_count, values.size].max unless ignoring_args? || (@expected_received_count == 0 and @at_least) if implementation # TODO: deprecate `and_return { value }` self.inner_implementation_action = implementation else self.terminal_implementation_action = AndReturnImplementation.new(values) end nil end |
- (Object) and_throw(symbol) - (Object) and_throw(symbol, object)
Tells the object to throw a symbol (with the object if that form is used) when the message is received.
149 150 151 152 |
# File 'lib/rspec/mocks/message_expectation.rb', line 149 def and_throw(*args) self.terminal_implementation_action = Proc.new { throw(*args) } nil end |
- (Object) and_yield(*args) {|@eval_context = Object.new.extend(RSpec::Mocks::InstanceExec)| ... }
Tells the object to yield one or more args to a block when the message is received.
160 161 162 163 164 165 |
# File 'lib/rspec/mocks/message_expectation.rb', line 160 def and_yield(*args, &block) yield @eval_context = Object.new.extend(RSpec::Mocks::InstanceExec) if block @args_to_yield << args self.initial_implementation_action = AndYieldImplementation.new(@args_to_yield, @eval_context, @error_generator) self end |
- (Object) any_number_of_times
Allows an expected message to be received any number of times.
364 365 366 367 368 369 |
# File 'lib/rspec/mocks/message_expectation.rb', line 364 def any_number_of_times(&block) RSpec.deprecate "any_number_of_times", :replacement => "stub" self.inner_implementation_action = block @expected_received_count = :any self end |
- (Object) at_least(n)
Constrain a message expectation to be received at least a specific number of times.
328 329 330 331 332 333 334 335 336 |
# File 'lib/rspec/mocks/message_expectation.rb', line 328 def at_least(n, &block) if n == 0 RSpec.deprecate "at_least(0) with should_receive", :replacement => "stub" end self.inner_implementation_action = block set_expected_received_count :at_least, n self end |
- (Object) at_most(n)
Constrain a message expectation to be received at most a specific number of times.
344 345 346 347 348 |
# File 'lib/rspec/mocks/message_expectation.rb', line 344 def at_most(n, &block) self.inner_implementation_action = block set_expected_received_count :at_most, n self end |
- (Object) exactly(n)
Constrain a message expectation to be received a specific number of times.
316 317 318 319 320 |
# File 'lib/rspec/mocks/message_expectation.rb', line 316 def exactly(n, &block) self.inner_implementation_action = block set_expected_received_count :exactly, n self end |
- (Object) expectation_count_type
265 266 267 268 269 |
# File 'lib/rspec/mocks/message_expectation.rb', line 265 def expectation_count_type return :at_least if @at_least return :at_most if @at_most return nil end |
- (Object) never
Expect a message not to be received at all.
376 377 378 379 380 |
# File 'lib/rspec/mocks/message_expectation.rb', line 376 def never ErrorGenerator.raise_double_negation_error("expect(obj)") if negative? @expected_received_count = 0 self end |
- (Object) once
Expect a message to be received exactly one time.
387 388 389 390 391 |
# File 'lib/rspec/mocks/message_expectation.rb', line 387 def once(&block) self.inner_implementation_action = block set_expected_received_count :exactly, 1 self end |
- (Object) ordered
Expect messages to be received in a specific order.
411 412 413 414 415 416 |
# File 'lib/rspec/mocks/message_expectation.rb', line 411 def ordered(&block) self.inner_implementation_action = block @order_group.register(self) @ordered = true self end |
- (Object) raise_out_of_order_error
276 277 278 |
# File 'lib/rspec/mocks/message_expectation.rb', line 276 def raise_out_of_order_error @error_generator.raise_out_of_order_error @message end |
- (Object) times
Syntactic sugar for exactly
, at_least
and at_most
357 358 359 360 |
# File 'lib/rspec/mocks/message_expectation.rb', line 357 def times(&block) self.inner_implementation_action = block self end |
- (Object) twice
Expect a message to be received exactly two times.
398 399 400 401 402 |
# File 'lib/rspec/mocks/message_expectation.rb', line 398 def twice(&block) self.inner_implementation_action = block set_expected_received_count :exactly, 2 self end |
- (Object) with(*args)
Constrains a stub or message expectation to invocations with specific arguments.
With a stub, if the message might be received with other args as well,
you should stub a default value first, and then stub or mock the same
message using with
to constrain to specific arguments.
A message expectation will fail if the message is received with different arguments.
304 305 306 307 308 |
# File 'lib/rspec/mocks/message_expectation.rb', line 304 def with(*args, &block) self.inner_implementation_action = block if block_given? unless args.empty? @argument_list_matcher = ArgumentListMatcher.new(*args, &block) self end |