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

Commit 39b6eb1

Browse files
committed
validate arguments to and_invoke
1 parent 06f7331 commit 39b6eb1

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

lib/rspec/mocks/message_expectation.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ def and_invoke(first_proc, *procs)
117117
end
118118

119119
procs.unshift(first_proc)
120+
if procs.any? { |p| !p.respond_to?(:call) }
121+
raise ArgumentError, "Arguments to `and_invoke` must be callable."
122+
end
123+
120124
@expected_received_count = [@expected_received_count, procs.size].max unless ignoring_args? || (@expected_received_count == 0 && @at_least)
121125
self.terminal_implementation_action = AndInvokeImplementation.new(procs)
122126

spec/rspec/mocks/and_invoke_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ module Mocks
1616
expect { allow(obj).to receive(:foo).and_invoke }.to raise_error(ArgumentError)
1717
end
1818
end
19+
20+
context 'when a non-callable are passed in any position' do
21+
let(:non_callable) { nil }
22+
let(:callable) { lambda { nil } }
23+
24+
it 'raises ArgumentError' do
25+
expect { allow(obj).to receive(:foo).and_invoke(non_callable) }
26+
.to raise_error(ArgumentError, "Arguments to `and_invoke` must be callable.")
27+
expect { allow(obj).to receive(:foo).and_invoke(callable, non_callable) }
28+
.to raise_error(ArgumentError, "Arguments to `and_invoke` must be callable.")
29+
end
30+
end
1931
end
2032
end
2133
end

0 commit comments

Comments
 (0)