Contents

This page comes from RSpec Mocks 2.99.4. It is not part of the 3.12.4 docs. See the latest RSpec Mocks docs.

allow with a simple return value

Use the allow method with the receive matcher on a test double or a real object to tell the object to return a value (or values) in response to a given message. Nothing happens if the message is never received.

Scenario: stub with no return value

  • Given a file named "example_spec.rb" with:
    ruby
    describe "a stub with no return value specified" do
      let(:collaborator) { double("collaborator") }
    
      it "returns nil" do
        allow(collaborator).to receive(:message)
        expect(collaborator.message).to be(nil)
      end
    end
  • When I run `rspec example_spec.rb`
  • Then the examples should all pass

Scenario: stubs with return values

  • Given a file named "example_spec.rb" with:
    ruby
    describe "a stub with a return value" do
      context "specified in a block" do
        it "returns the specified value" do
          collaborator = double("collaborator")
          allow(collaborator).to receive(:message) { :value }
          expect(collaborator.message).to eq(:value)
        end
      end
    
      context "specified with #and_return" do
        it "returns the specified value" do
          collaborator = double("collaborator")
          allow(collaborator).to receive(:message).and_return(:value)
          expect(collaborator.message).to eq(:value)
        end
      end
    end
  • When I run `rspec example_spec.rb`
  • Then the examples should all pass

Source: features/method_stubs/simple_return_value_with_allow.feature at v2.99.4, RSpec Mocks (MIT license).