Skip to content

Commit

Permalink
Merge pull request #1585 from rspec/extend-and_invoke-docs
Browse files Browse the repository at this point in the history
Extend and_invoke docs
  • Loading branch information
pirj committed Aug 27, 2024
2 parents 762aafb + d8dc85a commit fe5d31f
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions features/configuring_responses/mixed_responses.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ Feature: Mixed responses
callables to have different behavior for consecutive calls. The final callable will continue to be
called if the message is received additional times.

Note: The invoked callable will be invoked with the calls arguments, so it is recommended to
use a `lambda` or similar with the same arity as your method but you can use a `proc` if you
do not care about arity (e.g. when raising).
Note: The invoked callable will be supplied the calls arguments, including any blocks (so `yield`
et al will be supported). It is recommended to use a `lambda` or similar with the same arity
as your method but you can use a `proc` if you do not care about arity(e.g. when raising).

Scenario: Mixed responses
Given a file named "raises_and_then_returns.rb" with:
Expand All @@ -23,3 +23,22 @@ Feature: Mixed responses
"""
When I run `rspec raises_and_then_returns.rb`
Then the examples should all pass

Scenario: Block arguments
Given a file named "yields_and_raises.rb" with:
"""ruby
RSpec.describe "when the method is called multiple times" do
it "yields and then later raises" do
dbl = double
allow(dbl).to receive(:foo).and_invoke(
proc { |&block| block.call("foo") },
proc { raise "failure" }
)
dbl.foo { |yielded| expect(yielded).to eq("foo") }
expect { dbl.foo }.to raise_error("failure")
end
end
"""
When I run `rspec yields_and_raises.rb`
Then the examples should all pass

0 comments on commit fe5d31f

Please sign in to comment.