Logo: Relish

  1. Sign in

Project: RSpec Mocks 3.8

Expecting messages

Use expect(...).to receive(...) to expect a message on a test double. Unfulfilled
message expectations trigger failures when the example completes. You can also use
expect(...).not_to receive(...) to set a negative message expectation.

Scenarios
Failing positive message expectation
Given
a file named "unfulfilled_message_expectation_spec.rb" with:
RSpec.describe "An unfulfilled positive message expectation" do
  it "triggers a failure" do
    dbl = double("Some Collaborator")
    expect(dbl).to receive(:foo)
  end
end
When
I run rspec unfulfilled_message_expectation_spec.rb
Then
it should fail with:
  1) An unfulfilled positive message expectation triggers a failure
     Failure/Error: expect(dbl).to receive(:foo)

       (Double "Some Collaborator").foo(*(any args))
           expected: 1 time with any arguments
           received: 0 times with any arguments
Passing positive message expectation
Given
a file named "fulfilled_message_expectation_spec.rb" with:
RSpec.describe "A fulfilled positive message expectation" do
  it "passes" do
    dbl = double("Some Collaborator")
    expect(dbl).to receive(:foo)
    dbl.foo
  end
end
When
I run rspec fulfilled_message_expectation_spec.rb
Then
the examples should all pass
Failing negative message expectation
Given
a file named "negative_message_expectation_spec.rb" with:
RSpec.describe "A negative message expectation" do
  it "fails when the message is received" do
    dbl = double("Some Collaborator").as_null_object
    expect(dbl).not_to receive(:foo)
    dbl.foo
  end
end
When
I run rspec negative_message_expectation_spec.rb
Then
it should fail with:
  1) A negative message expectation fails when the message is received
     Failure/Error: dbl.foo

       (Double "Some Collaborator").foo(no args)
           expected: 0 times with any arguments
           received: 1 time
Passing negative message expectation
Given
a file named "negative_message_expectation_spec.rb" with:
RSpec.describe "A negative message expectation" do
  it "passes if the message is never received" do
    dbl = double("Some Collaborator").as_null_object
    expect(dbl).not_to receive(:foo)
  end
end
When
I run rspec negative_message_expectation_spec.rb
Then
the examples should all pass

Last published about 1 month ago by myronmarston.