From 83cff208c44e224629447782caa01a0398e13327 Mon Sep 17 00:00:00 2001 From: Phil Pirozhkov Date: Sat, 27 Jul 2024 00:12:09 +0300 Subject: [PATCH] Extend and_invoke docs There's no other ways certain things can be done. --- .../mixed_responses.feature | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/features/configuring_responses/mixed_responses.feature b/features/configuring_responses/mixed_responses.feature index 295eb250a..9fb67e3db 100644 --- a/features/configuring_responses/mixed_responses.feature +++ b/features/configuring_responses/mixed_responses.feature @@ -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 @@ -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