Skip to content

Commit

Permalink
Merge pull request #2514 from rspec/fix-ruby2-keywords-in-controller-…
Browse files Browse the repository at this point in the history
…spec-method-missing

Fix keyword arguments in dynamic methods in controller specs
  • Loading branch information
JonRowe committed Aug 14, 2021
2 parents e68175d + 6957188 commit dc0e2b5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Expand Up @@ -13,6 +13,8 @@ Bug Fixes:
* Fix `ActiveRecord::TestFixture#uses_transaction` by using example description
to replace example name rather than example in our monkey patched
`run_in_transaction?` method. (Stan Lo, #2495)
* Prevent keyword arguments being lost when methods are invoked dynamically
in controller specs. (Josh Cheek, #2509, #2514)

### 5.0.1 / 2021-03-18
[Full Changelog](https://github.com/rspec/rspec-rails/compare/v5.0.0...v5.0.1)
Expand Down
1 change: 1 addition & 0 deletions lib/rspec/rails/example/controller_example_group.rb
Expand Up @@ -176,6 +176,7 @@ def method_missing(method, *args, &block)
super
end
end
ruby2_keywords :method_missing if respond_to?(:ruby2_keywords, true)

included do
subject { controller }
Expand Down
17 changes: 17 additions & 0 deletions spec/rspec/rails/example/controller_example_group_spec.rb
Expand Up @@ -19,6 +19,23 @@ def group_for(klass)
expect(group.included_modules).to include(RSpec::Rails::Matchers::RoutingMatchers)
end

it "handles methods invoked via `method_missing` that use keywords" do
group =
RSpec::Core::ExampleGroup.describe ApplicationController do
def a_method(value:); value; end
def method_missing(_name, *_args, **kwargs, &_block); a_method(**kwargs); end

# This example requires prepend so that the `method_missing` definition
# from `ControllerExampleGroup` bubbles up to our artificial one, in reality
# this is likely to be either an internal RSpec dispatch or one from a 3rd
# party gem.
prepend ControllerExampleGroup
end
example = group.new

expect(example.call_a_method(value: :value)).to eq :value
end

context "with implicit subject" do
it "uses the controller as the subject" do
controller = double('controller')
Expand Down

0 comments on commit dc0e2b5

Please sign in to comment.