Method: RSpec::Mocks::ExampleMethods#have_received

Defined in:
lib/rspec/mocks/example_methods.rb

#have_received(method_name, &block) ⇒ Object

Note:

have_received(...).with(...) is unable to work properly when passed arguments are mutated after the spy records the received message.

Verifies that the given object received the expected message during the course of the test. On a spy objects or as null object doubles this works for any method, on other objects the method must have been stubbed beforehand in order for messages to be verified.

Stubbing and verifying messages received in this way implements the Test Spy pattern.

Examples:

invitation = double('invitation', accept: true)
user.accept_invitation(invitation)
expect(invitation).to have_received(:accept)

# You can also use most message expectations:
expect(invitation).to have_received(:accept).with(mailer).once

Parameters:

  • method_name (Symbol)

    name of the method expected to have been called.



281
282
283
# File 'lib/rspec/mocks/example_methods.rb', line 281

def have_received(method_name, &block)
  Matchers::HaveReceived.new(method_name, &block)
end