Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #326 from rspec/feature_for_allow
Documentation for allow (counterpart to stub in the expect syntax)
- Loading branch information
Showing
8 changed files
with
135 additions
and
33 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
features/message_expectations/allow_any_instance_of.feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
Feature: allow a message on any instance of a class | ||
|
||
Use `allow_any_instance_of(Class).to receive` when you want to configure how | ||
instances of the given class respond to a message without setting an | ||
expectation that the message will be received. | ||
|
||
Scenario: allowing a message on any instance of a class | ||
Given a file named "example_spec.rb" with: | ||
"""ruby | ||
describe "any_instance.should_receive" do | ||
before do | ||
allow_any_instance_of(Object).to receive(:foo).and_return(:return_value) | ||
end | ||
it "allows any instance of the class to receive the message" do | ||
o = Object.new | ||
expect(o.foo).to eq(:return_value) | ||
end | ||
it "passes even if no instances receive that message" do | ||
o = Object.new | ||
end | ||
end | ||
""" | ||
When I run `rspec example_spec.rb` | ||
Then the examples should all pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
features/method_stubs/simple_return_value_with_allow.feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
Feature: 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 |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters