Skip to content
This repository has been archived by the owner on Nov 30, 2024. It is now read-only.

Commit

Permalink
Extend and_invoke docs
Browse files Browse the repository at this point in the history
There's no other ways certain things can be done.
  • Loading branch information
pirj committed Jul 26, 2024
1 parent 762aafb commit 83cff20
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions features/configuring_responses/mixed_responses.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Feature: Mixed responses
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: You can also use `and_invoke` to yield. In this case, use a `proc` with a `&block`
parameter.

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

Scenario: Yielding
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 83cff20

Please sign in to comment.