This repository was archived by the owner on Nov 30, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 356
Add and_invoke
for sequential mixed (return/raise) responses.
#1411
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7a5e2c1
Add `and_invoke` for sequential mixed (return/raise) responses.
askreet 1f9d1b5
Replace -> with lambda, for 1.8.7 support.
askreet 06f7331
Apply suggestions from code review
askreet 39b6eb1
validate arguments to and_invoke
askreet 5aa1a35
treat procs as if they were blocks to and_return
askreet ffc4583
fix ruby 1.8 syntax error
askreet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,21 @@ | ||
Feature: Mixed responses | ||
|
||
Use `and_invoke` to invoke a Proc when a message is received. Pass `and_invoke` multiple | ||
Procs to have different behavior for consecutive calls. The final Proc will continue to be | ||
called if the message is received additional times. | ||
|
||
Scenario: Mixed responses | ||
Given a file named "raises_and_then_returns.rb" with: | ||
"""ruby | ||
RSpec.describe "when the method is called multiple times" do | ||
it "raises and then later returns a value" do | ||
dbl = double | ||
allow(dbl).to receive(:foo).and_invoke(lambda { raise "failure" }, lambda { true }) | ||
expect { dbl.foo }.to raise_error("failure") | ||
expect(dbl.foo).to eq(true) | ||
end | ||
end | ||
""" | ||
When I run `rspec raises_and_then_returns.rb` | ||
Then the examples should all pass |
This file contains hidden or 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 hidden or 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 hidden or 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,45 @@ | ||
module RSpec | ||
module Mocks | ||
RSpec.describe 'and_invoke' do | ||
let(:obj) { double('obj') } | ||
|
||
context 'when a block is passed' do | ||
it 'raises ArgumentError' do | ||
expect { | ||
allow(obj).to receive(:foo).and_invoke('bar') { 'baz' } | ||
}.to raise_error(ArgumentError, /implementation block/i) | ||
end | ||
end | ||
|
||
context 'when no argument is passed' do | ||
it 'raises ArgumentError' do | ||
expect { allow(obj).to receive(:foo).and_invoke }.to raise_error(ArgumentError) | ||
end | ||
end | ||
|
||
context 'when a non-callable are passed in any position' do | ||
let(:non_callable) { nil } | ||
let(:callable) { lambda { nil } } | ||
|
||
it 'raises ArgumentError' do | ||
error = [ArgumentError, "Arguments to `and_invoke` must be callable."] | ||
pirj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
expect { allow(obj).to receive(:foo).and_invoke(non_callable) }.to raise_error(*error) | ||
expect { allow(obj).to receive(:foo).and_invoke(callable, non_callable) }.to raise_error(*error) | ||
end | ||
end | ||
|
||
context 'when calling passed callables' do | ||
let(:dbl) { double } | ||
|
||
it 'passes the arguments into the callable' do | ||
expect(dbl).to receive(:square_then_cube).and_invoke(lambda { |i| i ** 2 }, | ||
pirj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
lambda { |i| i ** 3 }) | ||
|
||
expect(dbl.square_then_cube(2)).to eq 4 | ||
expect(dbl.square_then_cube(2)).to eq 8 | ||
end | ||
end | ||
pirj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
end | ||
end |
This file contains hidden or 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 hidden or 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,117 @@ | ||
module RSpec | ||
module Mocks | ||
RSpec.describe "a message expectation with multiple invoke handlers and no specified count" do | ||
let(:a_double) { double } | ||
|
||
before(:each) do | ||
expect(a_double).to receive(:do_something).and_invoke(lambda { 1 }, lambda { raise "2" }, lambda { 3 }) | ||
end | ||
|
||
it "invokes procs in order" do | ||
expect(a_double.do_something).to eq 1 | ||
expect { a_double.do_something }.to raise_error("2") | ||
expect(a_double.do_something).to eq 3 | ||
verify a_double | ||
end | ||
|
||
it "falls back to a previously stubbed value" do | ||
allow(a_double).to receive_messages :do_something => :stub_result | ||
expect(a_double.do_something).to eq 1 | ||
expect { a_double.do_something }.to raise_error("2") | ||
expect(a_double.do_something).to eq 3 | ||
expect(a_double.do_something).to eq :stub_result | ||
end | ||
|
||
it "fails when there are too few calls (if there is no stub)" do | ||
a_double.do_something | ||
expect { a_double.do_something }.to raise_error("2") | ||
expect { verify a_double }.to fail | ||
end | ||
|
||
it "fails when there are too many calls (if there is no stub)" do | ||
a_double.do_something | ||
expect { a_double.do_something }.to raise_error("2") | ||
a_double.do_something | ||
a_double.do_something | ||
expect { verify a_double }.to fail | ||
end | ||
end | ||
|
||
RSpec.describe "a message expectation with multiple invoke handlers with a specified count equal to the number of values" do | ||
let(:a_double) { double } | ||
|
||
before(:each) do | ||
expect(a_double).to receive(:do_something).exactly(3).times.and_invoke(lambda { 1 }, lambda { raise "2" }, lambda { 3 }) | ||
end | ||
|
||
it "returns values in order to consecutive calls" do | ||
expect(a_double.do_something).to eq 1 | ||
expect { a_double.do_something }.to raise_error("2") | ||
expect(a_double.do_something).to eq 3 | ||
verify a_double | ||
end | ||
end | ||
|
||
RSpec.describe "a message expectation with multiple invoke handlers specifying at_least less than the number of values" do | ||
let(:a_double) { double } | ||
|
||
before { expect(a_double).to receive(:do_something).at_least(:twice).with(no_args).and_invoke(lambda { 11 }, lambda { 22 }) } | ||
|
||
it "uses the last return value for subsequent calls" do | ||
expect(a_double.do_something).to equal(11) | ||
expect(a_double.do_something).to equal(22) | ||
expect(a_double.do_something).to equal(22) | ||
verify a_double | ||
end | ||
|
||
it "fails when called less than the specified number" do | ||
expect(a_double.do_something).to equal(11) | ||
expect { verify a_double }.to fail | ||
end | ||
|
||
context "when method is stubbed too" do | ||
before { allow(a_double).to receive(:do_something).and_invoke lambda { :stub_result } } | ||
|
||
it "uses the last value for subsequent calls" do | ||
expect(a_double.do_something).to equal(11) | ||
expect(a_double.do_something).to equal(22) | ||
expect(a_double.do_something).to equal(22) | ||
verify a_double | ||
end | ||
|
||
it "fails when called less than the specified number" do | ||
expect(a_double.do_something).to equal(11) | ||
expect { verify a_double }.to fail | ||
end | ||
end | ||
end | ||
|
||
RSpec.describe "a message expectation with multiple invoke handlers with a specified count larger than the number of values" do | ||
let(:a_double) { double } | ||
before { expect(a_double).to receive(:do_something).exactly(3).times.and_invoke(lambda { 11 }, lambda { 22 }) } | ||
|
||
it "uses the last return value for subsequent calls" do | ||
expect(a_double.do_something).to equal(11) | ||
expect(a_double.do_something).to equal(22) | ||
expect(a_double.do_something).to equal(22) | ||
verify a_double | ||
end | ||
|
||
it "fails when called less than the specified number" do | ||
a_double.do_something | ||
a_double.do_something | ||
expect { verify a_double }.to fail | ||
end | ||
|
||
it "fails fast when called greater than the specified number" do | ||
a_double.do_something | ||
a_double.do_something | ||
a_double.do_something | ||
|
||
expect_fast_failure_from(a_double) do | ||
a_double.do_something | ||
end | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The feature description says "if the message is received additional times", maybe add an additional line repeating this one to prove/indicate that?