Logo: Relish

  1. Sign up
  2. Sign in

Project: RSpec Mocks 3.7

Returning a value

Use and_return to specify a return value. Pass and_return multiple values to specify
different return values for consecutive calls. The final value will continue to be returned if
the message is received additional times.

Scenarios
Nil is returned by default
Given
a file named "returns_nil_spec.rb" with:
RSpec.describe "The default response" do
  it "returns nil when no response has been configured" do
    dbl = double
    allow(dbl).to receive(:foo)
    expect(dbl.foo).to be_nil
  end
end
When
I run rspec returns_nil_spec.rb
Then
the examples should all pass
Specify a return value
Given
a file named "and_return_spec.rb" with:
RSpec.describe "Specifying a return value" do
  it "returns the specified return value" do
    dbl = double
    allow(dbl).to receive(:foo).and_return(14)
    expect(dbl.foo).to eq(14)
  end
end
When
I run rspec and_return_spec.rb
Then
the examples should all pass
Specify different return values for multiple calls
Given
a file named "multiple_calls_spec.rb" with:
RSpec.describe "When the method is called multiple times" do
  it "returns the specified values in order, then keeps returning the last value" do
    dbl = double
    allow(dbl).to receive(:foo).and_return(1, 2, 3)

    expect(dbl.foo).to eq(1)
    expect(dbl.foo).to eq(2)
    expect(dbl.foo).to eq(3)
    expect(dbl.foo).to eq(3)
    expect(dbl.foo).to eq(3)
  end
end
When
I run rspec multiple_calls_spec.rb
Then
the examples should all pass

Last published about 1 month ago by myronmarston.