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

Commit 5aa1a35

Browse files
committed
treat procs as if they were blocks to and_return
1 parent 39b6eb1 commit 5aa1a35

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

lib/rspec/mocks/message_expectation.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -732,12 +732,14 @@ def initialize(procs_to_invoke)
732732
@procs_to_invoke = procs_to_invoke
733733
end
734734

735-
def call(*_args_to_ignore, &_block)
736-
if @procs_to_invoke.size > 1
737-
@procs_to_invoke.shift.call
738-
else
739-
@procs_to_invoke.first.call
740-
end
735+
def call(*args, &block)
736+
proc = if @procs_to_invoke.size > 1
737+
@procs_to_invoke.shift
738+
else
739+
@procs_to_invoke.first
740+
end
741+
742+
proc.call(*args, &block)
741743
end
742744
end
743745

spec/rspec/mocks/and_invoke_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ module Mocks
2828
.to raise_error(ArgumentError, "Arguments to `and_invoke` must be callable.")
2929
end
3030
end
31+
32+
context 'when calling passed callables' do
33+
let(:dbl) { double }
34+
35+
it 'passes the arguments into the callable' do
36+
expect(dbl).to receive(:square_then_cube).and_invoke(lambda { |i| i ** 2 },
37+
lambda { |i| i ** 3 })
38+
39+
expect(dbl.square_then_cube(2)).to eq 4
40+
expect(dbl.square_then_cube(2)).to eq 8
41+
end
42+
end
3143
end
3244
end
3345
end

0 commit comments

Comments
 (0)