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

Commit 585bc7e

Browse files
committed
Add tests (and feature sniffing) for mocking kwarg methods
1 parent b174c04 commit 585bc7e

File tree

2 files changed

+80
-9
lines changed

2 files changed

+80
-9
lines changed

spec/rspec/mocks/and_call_original_spec.rb

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,24 @@ def meth_2(x)
1212
yield x, :additional_yielded_arg
1313
end
1414

15-
def meth_3(x:)
16-
x
15+
if RSpec::Support::RubyFeatures.kw_args_supported?
16+
binding.eval(<<-RUBY, __FILE__, __LINE__)
17+
def meth_3(**kwargs)
18+
kwargs
19+
end
20+
21+
def meth_4(x: 1)
22+
x
23+
end
24+
RUBY
25+
end
26+
27+
if RSpec::Support::RubyFeatures.required_kw_args_supported?
28+
binding.eval(<<-RUBY, __FILE__, __LINE__)
29+
def meth_5(x:)
30+
x
31+
end
32+
RUBY
1733
end
1834

1935
def self.new_instance
@@ -129,9 +145,32 @@ def inst.foo; :bar; end
129145
expect(klass.new.meth_1).to eq(:original)
130146
end
131147

132-
it 'works for instance methods that use keyword arguments' do
133-
expect_any_instance_of(klass).to receive(:meth_3).and_call_original
134-
expect(klass.new.meth_3(x: :kwarg)).to eq(:kwarg)
148+
if RSpec::Support::RubyFeatures.kw_args_supported?
149+
binding.eval(<<-RUBY, __FILE__, __LINE__)
150+
it 'works for instance methods that use double splat' do
151+
expect_any_instance_of(klass).to receive(:meth_3).and_call_original
152+
expect(klass.new.meth_3(x: :kwarg)).to eq({x: :kwarg})
153+
end
154+
155+
it 'works for instance methods that use optional keyword arguments' do
156+
expect_any_instance_of(klass).to receive(:meth_4).and_call_original
157+
expect(klass.new.meth_4).to eq(1)
158+
end
159+
160+
it 'works for instance methods that use optional keyword arguments with an argument supplied' do
161+
expect_any_instance_of(klass).to receive(:meth_4).and_call_original
162+
expect(klass.new.meth_4(x: :kwarg)).to eq(:kwarg)
163+
end
164+
RUBY
165+
end
166+
167+
if RSpec::Support::RubyFeatures.required_kw_args_supported?
168+
binding.eval(<<-RUBY, __FILE__, __LINE__)
169+
it 'works for instance methods that use required keyword arguments' do
170+
expect_any_instance_of(klass).to receive(:meth_5).and_call_original
171+
expect(klass.new.meth_5(x: :kwarg)).to eq(:kwarg)
172+
end
173+
RUBY
135174
end
136175

137176
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
@@ -62,12 +62,44 @@ module Mocks
6262
expect(receiver.foo).to eq(4)
6363
end
6464

65-
it 'allows a `do...end` block implementation with keyword args to be provided' do
66-
wrapped.to receive(:foo) do |**kwargs|
67-
kwargs[:kw]
65+
if RSpec::Support::RubyFeatures.kw_args_supported?
66+
binding.eval(<<-RUBY, __FILE__, __LINE__)
67+
it 'allows a `do...end` block implementation with keyword args to be provided' do
68+
wrapped.to receive(:foo) do |**kwargs|
69+
kwargs[:kw]
70+
end
71+
72+
expect(receiver.foo(kw: :arg)).to eq(:arg)
73+
end
74+
75+
it 'allows a `do...end` block implementation with optional keyword args to be provided' do
76+
wrapped.to receive(:foo) do |kw: :arg|
77+
kw
78+
end
79+
80+
expect(receiver.foo(kw: 1)).to eq(1)
81+
end
82+
83+
it 'allows a `do...end` block implementation with optional keyword args to be provided' do
84+
wrapped.to receive(:foo) do |kw: :arg|
85+
kw
86+
end
87+
88+
expect(receiver.foo).to eq(:arg)
6889
end
90+
RUBY
91+
end
6992

70-
expect(receiver.foo(kw: :arg)).to eq(:arg)
93+
if RSpec::Support::RubyFeatures.required_kw_args_supported?
94+
binding.eval(<<-RUBY, __FILE__, __LINE__)
95+
it 'allows a `do...end` block implementation with required keyword args' do
96+
wrapped.to receive(:foo) do |kw:|
97+
kw
98+
end
99+
100+
expect(receiver.foo(kw: :arg)).to eq(:arg)
101+
end
102+
RUBY
71103
end
72104

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

0 commit comments

Comments
 (0)