Skip to content

Commit ef97e6b

Browse files
committed
Merge pull request #2514 from rspec/fix-ruby2-keywords-in-controller-spec-method-missing
Fix keyword arguments in dynamic methods in controller specs
1 parent 6cc0705 commit ef97e6b

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Bug Fixes:
88
* Fix `ActiveRecord::TestFixture#uses_transaction` by using example description
99
to replace example name rather than example in our monkey patched
1010
`run_in_transaction?` method. (Stan Lo, #2495)
11+
* Prevent keyword arguments being lost when methods are invoked dynamically
12+
in controller specs. (Josh Cheek, #2509, #2514)
1113

1214
### 5.0.1 / 2021-03-18
1315
[Full Changelog](https://github.com/rspec/rspec-rails/compare/v5.0.0...v5.0.1)

lib/rspec/rails/example/controller_example_group.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ def method_missing(method, *args, &block)
176176
super
177177
end
178178
end
179+
ruby2_keywords :method_missing if respond_to?(:ruby2_keywords, true)
179180

180181
included do
181182
subject { controller }

spec/rspec/rails/example/controller_example_group_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@ def group_for(klass)
1919
expect(group.included_modules).to include(RSpec::Rails::Matchers::RoutingMatchers)
2020
end
2121

22+
it "handles methods invoked via `method_missing` that use keywords" do
23+
group =
24+
RSpec::Core::ExampleGroup.describe ApplicationController do
25+
def a_method(value:); value; end
26+
def method_missing(_name, *_args, **kwargs, &_block); a_method(**kwargs); end
27+
28+
# This example requires prepend so that the `method_missing` definition
29+
# from `ControllerExampleGroup` bubbles up to our artificial one, in reality
30+
# this is likely to be either an internal RSpec dispatch or one from a 3rd
31+
# party gem.
32+
prepend ControllerExampleGroup
33+
end
34+
example = group.new
35+
36+
expect(example.call_a_method(value: :value)).to eq :value
37+
end
38+
2239
context "with implicit subject" do
2340
it "uses the controller as the subject" do
2441
controller = double('controller')

0 commit comments

Comments
 (0)