Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 56 lines (48 sloc) 1.818 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
Feature: stub with a simple return value

Use the `stub` method on a test double or a real object to tell the object to
return a value (or values) in response to a given message. If the message is
never received, nothing happens.

Scenario: simple stub with no return value
Given a file named "example_spec.rb" with:
"""
describe "a simple stub with no return value specified" do
let(:receiver) { double("receiver") }

it "returns nil" do
receiver.stub(:message)
receiver.message.should be(nil)
end

it "quietly carries on when not called" do
receiver.stub(:message)
end
end
"""
When I run `rspec example_spec.rb`
Then the output should contain "0 failures"

Scenario: single return value
Given a file named "example_spec.rb" with:
"""
describe "a simple stub with a return value" do
context "specified in a block" do
it "returns the specified value" do
receiver = double("receiver")
receiver.stub(:message) { :return_value }
receiver.message.should eq(:return_value)
end
end

context "specified in the double declaration" do
it "returns the specified value" do
receiver = double("receiver", :message => :return_value)
receiver.message.should eq(:return_value)
end
end

context "specified with and_return" do
it "returns the specified value" do
receiver = double("receiver")
receiver.stub(:message).and_return(:return_value)
receiver.message.should eq(:return_value)
end
end
end
"""
When I run `rspec example_spec.rb`
Then the output should contain "0 failures"
Something went wrong with that request. Please try again.