Skip to content

Commit

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

Fix keyword arguments in dynamic methods in controller specs
  • Loading branch information
JonRowe committed Aug 14, 2021
1 parent 6cc0705 commit ef97e6b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 ef97e6b

Please sign in to comment.