Class: RSpec::Mocks::MessageExpectation
Relationships & Source Files | |
Namespace Children | |
Modules:
| |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
RSpec::Mocks::VerifyingMessageExpectation
|
|
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
self,
ImplementationDetails
|
|
Inherits: | Object |
Defined in: | rspec-mocks/lib/rspec/mocks/message_expectation.rb |
Overview
Represents an individual method stub or message expectation. The methods defined here can be used to configure how it behaves. The methods return self
so that they can be chained together to form a fluent interface.
Configuring Responses
-
#and_call_original ⇒ nil
Tells the object to delegate to the original unmodified method when it receives the message.
-
#and_invoke(first_proc, *procs, &_block) ⇒ nil
Tells the object to invoke a Proc when it receives the message.
-
#and_raise ⇒ nil
Tells the object to raise an exception when the message is received.
-
#and_return(value) ⇒ nil
Tells the object to return a value when it receives the message.
-
#and_throw(symbol) ⇒ nil
Tells the object to throw a symbol (with the object if that form is used) when the message is received.
-
#and_wrap_original(&block) ⇒ nil
Decorates the stubbed method with the supplied block.
-
#and_yield(*args) {|@eval_context = Object.new| ... } ⇒ MessageExpectation
Tells the object to yield one or more args to a block when the message is received.
Constraining Receive Counts
-
#at_least(n, &block) ⇒ MessageExpectation
Constrain a message expectation to be received at least a specific number of times.
-
#at_most(n, &block) ⇒ MessageExpectation
Constrain a message expectation to be received at most a specific number of times.
-
#exactly(n, &block) ⇒ MessageExpectation
Constrain a message expectation to be received a specific number of times.
-
#never ⇒ MessageExpectation
Expect a message not to be received at all.
-
#once(&block) ⇒ MessageExpectation
Expect a message to be received exactly one time.
-
#thrice(&block) ⇒ MessageExpectation
Expect a message to be received exactly three times.
-
#time(&block)
Alias for #times.
- #times(&block) ⇒ MessageExpectation (also: #time)
-
#twice(&block) ⇒ MessageExpectation
Expect a message to be received exactly two times.
Other Constraints
-
#inspect
Alias for #to_s.
-
#ordered(&block) ⇒ MessageExpectation
Expect messages to be received in a specific order.
- #to_s ⇒ String (also: #inspect)
-
#with(*args, &block) ⇒ MessageExpectation
Constrains a stub or message expectation to invocations with specific arguments.
Instance Attribute Summary
ImplementationDetails
- Included
Instance Method Summary
ImplementationDetails
- Included
Instance Method Details
#and_call_original ⇒ nil
This is only available on partial doubles.
Tells the object to delegate to the original unmodified method when it receives the message.
# File 'rspec-mocks/lib/rspec/mocks/message_expectation.rb', line 141
def and_call_original block = lambda do |original, *args, &b| original.call(*args, &b) end block = block.ruby2_keywords if block.respond_to?(:ruby2_keywords) wrap_original(__method__, &block) end
#and_invoke(first_proc, *procs, &_block) ⇒ nil
Tells the object to invoke a Proc when it receives the message. Given more than one value, the result of the first Proc is returned the first time the message is received, the result of the second Proc is returned the next time, etc, etc.
If the message is received more times than there are Procs, the result of the last Proc is returned for every subsequent call.
# File 'rspec-mocks/lib/rspec/mocks/message_expectation.rb', line 109
def and_invoke(first_proc, *procs, &_block) raise_already_invoked_error_if_necessary(__method__) if negative? raise "`and_invoke` is not supported with negative message expectations" end if block_given? raise ArgumentError, "Implementation blocks aren't supported with `and_invoke`" end procs.unshift(first_proc) if procs.any? { |p| !p.respond_to?(:call) } raise ArgumentError, "Arguments to `and_invoke` must be callable." end @expected_received_count = [@expected_received_count, procs.size].max unless ignoring_args? || (@expected_received_count == 0 && @at_least) self.terminal_implementation_action = AndInvokeImplementation.new(procs) nil end
#and_raise ⇒ nil
#and_raise(ExceptionClass) ⇒ nil
#and_raise(ExceptionClass, message) ⇒ nil
#and_raise(exception_instance) ⇒ nil
nil
#and_raise(ExceptionClass) ⇒ nil
#and_raise(ExceptionClass, message) ⇒ nil
#and_raise(exception_instance) ⇒ nil
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.
# File 'rspec-mocks/lib/rspec/mocks/message_expectation.rb', line 186
def and_raise(*args) raise_already_invoked_error_if_necessary(__method__) self.terminal_implementation_action = Proc.new { raise(*args) } nil end
#and_return(value) ⇒ nil
#and_return(first_value, second_value) ⇒ nil
nil
#and_return(first_value, second_value) ⇒ nil
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 returned for every subsequent call.
# File 'rspec-mocks/lib/rspec/mocks/message_expectation.rb', line 71
def and_return(first_value, *values, &_block) raise_already_invoked_error_if_necessary(__method__) if negative? raise "`and_return` is not supported with negative message expectations" end if block_given? raise ArgumentError, "Implementation blocks aren't supported with `and_return`" end values.unshift(first_value) @expected_received_count = [@expected_received_count, values.size].max unless ignoring_args? || (@expected_received_count == 0 && @at_least) self.terminal_implementation_action = AndReturnImplementation.new(values) nil end
#and_throw(symbol) ⇒ nil
#and_throw(symbol, object) ⇒ nil
nil
#and_throw(symbol, object) ⇒ nil
Tells the object to throw a symbol (with the object if that form is used) when the message is received.
# File 'rspec-mocks/lib/rspec/mocks/message_expectation.rb', line 202
def and_throw(*args) raise_already_invoked_error_if_necessary(__method__) self.terminal_implementation_action = Proc.new { throw(*args) } nil end
#and_wrap_original(&block) ⇒ nil
This is only available on partial doubles.
Decorates the stubbed method with the supplied block. The original unmodified method is passed to the block along with any method call arguments so you can delegate to it, whilst still being able to change what args are passed to it and/or change the return value.
# File 'rspec-mocks/lib/rspec/mocks/message_expectation.rb', line 162
def and_wrap_original(&block) wrap_original(__method__, &block) end
#and_yield(*args) {|@eval_context = Object.new| ... } ⇒ MessageExpectation
Tells the object to yield one or more args to a block when the message is received.
# File 'rspec-mocks/lib/rspec/mocks/message_expectation.rb', line 214
def and_yield(*args, &block) raise_already_invoked_error_if_necessary(__method__) yield @eval_context = Object.new if block # Initialize args to yield now that it's being used, see also: comment # in constructor. @args_to_yield ||= [] @args_to_yield << args self.initial_implementation_action = AndYieldImplementation.new(@args_to_yield, @eval_context, @error_generator) self end
#at_least(n, &block) ⇒ MessageExpectation
Constrain a message expectation to be received at least a specific number of times.
# File 'rspec-mocks/lib/rspec/mocks/message_expectation.rb', line 249
def at_least(n, &block) raise_already_invoked_error_if_necessary(__method__) set_expected_received_count :at_least, n if n == 0 raise "at_least(0) has been removed, use allow(...).to receive(:message) instead" end self.inner_implementation_action = block self end
#at_most(n, &block) ⇒ MessageExpectation
Constrain a message expectation to be received at most a specific number of times.
# File 'rspec-mocks/lib/rspec/mocks/message_expectation.rb', line 268
def at_most(n, &block) raise_already_invoked_error_if_necessary(__method__) self.inner_implementation_action = block set_expected_received_count :at_most, n self end
#exactly(n, &block) ⇒ MessageExpectation
Constrain a message expectation to be received a specific number of times.
# File 'rspec-mocks/lib/rspec/mocks/message_expectation.rb', line 236
def exactly(n, &block) raise_already_invoked_error_if_necessary(__method__) self.inner_implementation_action = block set_expected_received_count :exactly, n self end
#inspect
Alias for #to_s.
# File 'rspec-mocks/lib/rspec/mocks/message_expectation.rb', line 401
alias inspect to_s
#never ⇒ MessageExpectation
Expect a message not to be received at all.
# File 'rspec-mocks/lib/rspec/mocks/message_expectation.rb', line 293
def never error_generator.raise_double_negation_error("expect(obj)") if negative? @expected_received_count = 0 self end
#once(&block) ⇒ MessageExpectation
Expect a message to be received exactly one time.
# File 'rspec-mocks/lib/rspec/mocks/message_expectation.rb', line 304
def once(&block) self.inner_implementation_action = block set_expected_received_count :exactly, 1 self end
#ordered(&block) ⇒ MessageExpectation
Expect messages to be received in a specific order.
# File 'rspec-mocks/lib/rspec/mocks/message_expectation.rb', line 379
def ordered(&block) if type == :stub RSpec.warning( "`allow(...).to receive(..).ordered` is not supported and will " \ "have no effect, use `and_return(*ordered_values)` instead." ) end self.inner_implementation_action = block additional_expected_calls.times do @order_group.register(self) end @ordered = true self end
#thrice(&block) ⇒ MessageExpectation
Expect a message to be received exactly three times.
# File 'rspec-mocks/lib/rspec/mocks/message_expectation.rb', line 326
def thrice(&block) self.inner_implementation_action = block set_expected_received_count :exactly, 3 self end
#time(&block)
Alias for #times.
# File 'rspec-mocks/lib/rspec/mocks/message_expectation.rb', line 286
alias time times
#times(&block) ⇒ MessageExpectation
Also known as: #time
[ GitHub ]
# File 'rspec-mocks/lib/rspec/mocks/message_expectation.rb', line 282
def times(&block) self.inner_implementation_action = block self end
#to_s ⇒ String
Also known as: #inspect
# File 'rspec-mocks/lib/rspec/mocks/message_expectation.rb', line 396
def to_s args_description = error_generator.method_call_args_description(@argument_list_matcher.expected_args, "", "") { true } args_description = "(#{args_description})" unless args_description.start_with?("(") "#<#{self.class} #{error_generator.intro}.#{}#{args_description}>" end
#twice(&block) ⇒ MessageExpectation
Expect a message to be received exactly two times.
# File 'rspec-mocks/lib/rspec/mocks/message_expectation.rb', line 315
def twice(&block) self.inner_implementation_action = block set_expected_received_count :exactly, 2 self end
#with(*args, &block) ⇒ MessageExpectation
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.
# File 'rspec-mocks/lib/rspec/mocks/message_expectation.rb', line 359
def with(*args, &block) raise_already_invoked_error_if_necessary(__method__) if args.empty? raise ArgumentError, "`with` must have at least one argument. Use `no_args` matcher to set the expectation of receiving no arguments." end self.inner_implementation_action = block @argument_list_matcher = ArgumentListMatcher.new(*args) self end