-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[mocks] Merge pull request rspec/rspec-mocks#1411 from askreet/and_in…
…voke Add `and_invoke` for sequential mixed (return/raise) responses. --- This commit was imported from rspec/rspec-mocks@70fbe12.
- Loading branch information
Showing
8 changed files
with
254 additions
and
4 deletions.
There are no files selected for viewing
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
21 changes: 21 additions & 0 deletions
21
rspec-mocks/features/configuring_responses/mixed_responses.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,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 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
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."] | ||
|
||
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 }, | ||
lambda { |i| i ** 3 }) | ||
|
||
expect(dbl.square_then_cube(2)).to eq 4 | ||
expect(dbl.square_then_cube(2)).to eq 8 | ||
end | ||
end | ||
end | ||
end | ||
end |
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
117 changes: 117 additions & 0 deletions
117
rspec-mocks/spec/rspec/mocks/multiple_invoke_handler_spec.rb
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,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 |