Class: RSpec::Mocks::MessageExpectation
- Inherits:
-
Object
- Object
- RSpec::Mocks::MessageExpectation
- Defined in:
- lib/rspec/mocks/message_expectation.rb
Instance Attribute Summary collapse
-
#argument_list_matcher ⇒ Object
writeonly
Sets the attribute argument_list_matcher.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
Instance Method Summary collapse
-
#and_raise(exception = RuntimeError) ⇒ Object
Tells the object to raise an exception when the message is received.
-
#and_return(*values, &implementation) ⇒ Object
Tells the object to return a value when it receives the message.
-
#and_throw(symbol, object = nil) ⇒ Object
Tells the object to throw a symbol (with the object if that form is used) when the message is received.
-
#and_yield(*args) {|@eval_context = Object.new.extend(RSpec::Mocks::InstanceExec)| ... } ⇒ Object
Tells the object to yield one or more args to a block when the message is received.
-
#any_number_of_times(&block) ⇒ Object
Allows an expected message to be received any number of times.
-
#at_least(n, &block) ⇒ Object
Constrain a message expectation to be received at least a specific number of times.
-
#at_most(n, &block) ⇒ Object
Constrain a message expectation to be received at most a specific number of times.
-
#exactly(n, &block) ⇒ Object
Constrain a message expectation to be received a specific number of times.
-
#never ⇒ Object
Expect a message not to be received at all.
-
#once(&block) ⇒ Object
Expect a message to be received exactly one time.
-
#ordered(&block) ⇒ Object
Expect messages to be received in a specific order.
- #raise_out_of_order_error ⇒ Object
-
#times(&block) ⇒ Object
Syntactic sugar for
exactly,at_leastandat_most. -
#twice(&block) ⇒ Object
Expect a message to be received exactly two times.
-
#with(*args, &block) ⇒ Object
Constrains a stub or message expectation to invocations with specific arguments.
Instance Attribute Details
#argument_list_matcher=(value) ⇒ Object (writeonly)
Sets the attribute argument_list_matcher
8 9 10 |
# File 'lib/rspec/mocks/message_expectation.rb', line 8 def argument_list_matcher=(value) @argument_list_matcher = value end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
7 8 9 |
# File 'lib/rspec/mocks/message_expectation.rb', line 7 def end |
Instance Method Details
#and_raise ⇒ Object #and_raise(ExceptionClass) ⇒ Object #and_raise(exception_instance) ⇒ Object
When you pass an exception class, the MessageExpectation will raise
an instance of it, creating it with new. If the exception class
initializer requires any parameters, you must pass in an instance and
not the class.
Tells the object to raise an exception when the message is received.
118 119 120 |
# File 'lib/rspec/mocks/message_expectation.rb', line 118 def and_raise(exception=RuntimeError) @exception_to_raise = exception end |
#and_return(value) ⇒ Object #and_return(first_value, second_value) ⇒ Object #and_return(&block) ⇒ Object
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.
94 95 96 97 98 |
# File 'lib/rspec/mocks/message_expectation.rb', line 94 def and_return(*values, &implementation) @expected_received_count = [@expected_received_count, values.size].max unless ignoring_args? || (@expected_received_count == 0 and @at_least) @consecutive = true if values.size > 1 @implementation = implementation || build_implementation(values) end |
#and_throw(symbol) ⇒ Object #and_throw(symbol, object) ⇒ Object
Tells the object to throw a symbol (with the object if that form is used) when the message is received.
132 133 134 |
# File 'lib/rspec/mocks/message_expectation.rb', line 132 def and_throw(symbol, object = nil) @args_to_throw = [symbol, object].compact end |
#and_yield(*args) {|@eval_context = Object.new.extend(RSpec::Mocks::InstanceExec)| ... } ⇒ Object
Tells the object to yield one or more args to a block when the message is received.
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/rspec/mocks/message_expectation.rb', line 142 def and_yield(*args, &block) if @args_to_yield_were_cloned @args_to_yield.clear @args_to_yield_were_cloned = false end yield @eval_context = Object.new.extend(RSpec::Mocks::InstanceExec) if block @args_to_yield << args self end |
#any_number_of_times(&block) ⇒ Object
Allows an expected message to be received any number of times.
349 350 351 352 353 |
# File 'lib/rspec/mocks/message_expectation.rb', line 349 def any_number_of_times(&block) @implementation = block if block @expected_received_count = :any self end |
#at_least(n, &block) ⇒ Object
Constrain a message expectation to be received at least a specific number of times.
317 318 319 320 321 |
# File 'lib/rspec/mocks/message_expectation.rb', line 317 def at_least(n, &block) @implementation = block if block set_expected_received_count :at_least, n self end |
#at_most(n, &block) ⇒ Object
Constrain a message expectation to be received at most a specific number of times.
329 330 331 332 333 |
# File 'lib/rspec/mocks/message_expectation.rb', line 329 def at_most(n, &block) @implementation = block if block set_expected_received_count :at_most, n self end |
#exactly(n, &block) ⇒ Object
Constrain a message expectation to be received a specific number of times.
305 306 307 308 309 |
# File 'lib/rspec/mocks/message_expectation.rb', line 305 def exactly(n, &block) @implementation = block if block set_expected_received_count :exactly, n self end |
#never ⇒ Object
Expect a message not to be received at all.
360 361 362 363 |
# File 'lib/rspec/mocks/message_expectation.rb', line 360 def never @expected_received_count = 0 self end |
#once(&block) ⇒ Object
Expect a message to be received exactly one time.
370 371 372 373 374 |
# File 'lib/rspec/mocks/message_expectation.rb', line 370 def once(&block) @implementation = block if block set_expected_received_count :exactly, 1 self end |
#ordered(&block) ⇒ Object
Expect messages to be received in a specific order.
394 395 396 397 398 399 |
# File 'lib/rspec/mocks/message_expectation.rb', line 394 def ordered(&block) @implementation = block if block @order_group.register(self) @ordered = true self end |
#raise_out_of_order_error ⇒ Object
265 266 267 |
# File 'lib/rspec/mocks/message_expectation.rb', line 265 def raise_out_of_order_error @error_generator.raise_out_of_order_error end |
#times(&block) ⇒ Object
Syntactic sugar for exactly, at_least and at_most
342 343 344 345 |
# File 'lib/rspec/mocks/message_expectation.rb', line 342 def times(&block) @implementation = block if block self end |
#twice(&block) ⇒ Object
Expect a message to be received exactly two times.
381 382 383 384 385 |
# File 'lib/rspec/mocks/message_expectation.rb', line 381 def twice(&block) @implementation = block if block set_expected_received_count :exactly, 2 self end |
#with(*args, &block) ⇒ Object
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.
293 294 295 296 297 |
# File 'lib/rspec/mocks/message_expectation.rb', line 293 def with(*args, &block) @implementation = block if block_given? unless args.empty? @argument_list_matcher = ArgumentListMatcher.new(*args, &block) self end |