Class | RSpec::Mocks::MessageExpectation |
In: |
lib/rspec/mocks/message_expectation.rb
|
Parent: | Object |
error_generator | [RW] | |
expected_from | [W] | |
expected_received_count | [W] | |
method_block | [W] | |
sym | [R] | @private |
@overload and_raise @overload and_raise(ExceptionClass) @overload and_raise(exception_instance)
Tells the object to raise an exception when the message is received.
@note
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.
@example
car.stub(:go).and_raise car.stub(:go).and_raise(OutOfGas) car.stub(:go).and_raise(OutOfGas.new(2, :oz))
@overload and_return(value) @overload and_return(first_value, second_value) @overload 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.
@example
counter.stub(:count).and_return(1) counter.count # => 1 counter.count # => 1 counter.stub(:count).and_return(1,2,3) counter.count # => 1 counter.count # => 2 counter.count # => 3 counter.count # => 3 counter.count # => 3 # etc # Supported, but ... counter.stub(:count).and_return { 1 } counter.count # => 1 # ... this is prefered counter.stub(:count) { 1 } counter.count # => 1
Tells the object to yield one or more args to a block when the message is received.
@example
stream.stub(:open).and_yield(StringIO.new)
Constrain a message expectation to be received at least a specific number of times.
@example
dealer.should_recieve(:deal_card).at_least(9).times
Constrain a message expectation to be received at most a specific number of times.
@example
dealer.should_recieve(:deal_card).at_most(10).times
Constrain a message expectation to be received a specific number of times.
@example
dealer.should_recieve(:deal_card).exactly(10).times
Expect messages to be received in a specific order.
@example
api.should_receive(:prepare).ordered api.should_receive(:run).ordered api.should_receive(:finish).ordered
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.
@example
cart.stub(:add) { :failure } cart.stub(:add).with(Book.new(:isbn => 1934356379)) { :success } cart.add(Book.new(:isbn => 1234567890)) # => :failure cart.add(Book.new(:isbn => 1934356379)) # => :success cart.should_receive(:add).with(Book.new(:isbn => 1934356379)) { :success } cart.add(Book.new(:isbn => 1234567890)) # => failed expectation cart.add(Book.new(:isbn => 1934356379)) # => passes