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

Commit a03b484

Browse files
jhottensteinpirj
authored andcommitted
Add tests (and feature sniffing) for mocking kwarg methods
1 parent d8f272e commit a03b484

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

spec/rspec/mocks/and_call_original_spec.rb

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@ def meth_2(x)
1212
yield x, :additional_yielded_arg
1313
end
1414

15-
def meth_3(x:)
15+
def meth_3(**kwargs)
16+
kwargs
17+
end
18+
19+
def meth_4(x: 1)
20+
x
21+
end
22+
23+
def meth_5(x:)
1624
x
1725
end
1826

@@ -129,9 +137,24 @@ def inst.foo; :bar; end
129137
expect(klass.new.meth_1).to eq(:original)
130138
end
131139

132-
it 'works for instance methods that use keyword arguments' do
140+
it 'works for instance methods that use double splat' do
133141
expect_any_instance_of(klass).to receive(:meth_3).and_call_original
134-
expect(klass.new.meth_3(x: :kwarg)).to eq(:kwarg)
142+
expect(klass.new.meth_3(x: :kwarg)).to eq({x: :kwarg})
143+
end
144+
145+
it 'works for instance methods that use optional keyword arguments' do
146+
expect_any_instance_of(klass).to receive(:meth_4).and_call_original
147+
expect(klass.new.meth_4).to eq(1)
148+
end
149+
150+
it 'works for instance methods that use optional keyword arguments with an argument supplied' do
151+
expect_any_instance_of(klass).to receive(:meth_4).and_call_original
152+
expect(klass.new.meth_4(x: :kwarg)).to eq(:kwarg)
153+
end
154+
155+
it 'works for instance methods that use required keyword arguments' do
156+
expect_any_instance_of(klass).to receive(:meth_5).and_call_original
157+
expect(klass.new.meth_5(x: :kwarg)).to eq(:kwarg)
135158
end
136159

137160
it 'works for instance methods defined on the superclass of the class' do

spec/rspec/mocks/matchers/receive_spec.rb

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,44 @@ module Mocks
3232
expect(receiver.foo).to eq(4)
3333
end
3434

35-
it 'allows a `do...end` block implementation with keyword args to be provided' do
36-
wrapped.to receive(:foo) do |**kwargs|
37-
kwargs[:kw]
35+
if RSpec::Support::RubyFeatures.kw_args_supported?
36+
binding.eval(<<-RUBY, __FILE__, __LINE__)
37+
it 'allows a `do...end` block implementation with keyword args to be provided' do
38+
wrapped.to receive(:foo) do |**kwargs|
39+
kwargs[:kw]
40+
end
41+
42+
expect(receiver.foo(kw: :arg)).to eq(:arg)
43+
end
44+
45+
it 'allows a `do...end` block implementation with optional keyword args to be provided' do
46+
wrapped.to receive(:foo) do |kw: :arg|
47+
kw
48+
end
49+
50+
expect(receiver.foo(kw: 1)).to eq(1)
51+
end
52+
53+
it 'allows a `do...end` block implementation with optional keyword args to be provided' do
54+
wrapped.to receive(:foo) do |kw: :arg|
55+
kw
56+
end
57+
58+
expect(receiver.foo).to eq(:arg)
3859
end
60+
RUBY
61+
end
3962

40-
expect(receiver.foo(kw: :arg)).to eq(:arg)
63+
if RSpec::Support::RubyFeatures.required_kw_args_supported?
64+
binding.eval(<<-RUBY, __FILE__, __LINE__)
65+
it 'allows a `do...end` block implementation with required keyword args' do
66+
wrapped.to receive(:foo) do |kw:|
67+
kw
68+
end
69+
70+
expect(receiver.foo(kw: :arg)).to eq(:arg)
71+
end
72+
RUBY
4173
end
4274

4375
it 'allows chaining off a `do...end` block implementation to be provided' do

0 commit comments

Comments
 (0)