Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix keyword arguments in dynamic methods in controller specs #2514

Merged
merged 3 commits into from Aug 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
JonRowe marked this conversation as resolved.
Show resolved Hide resolved

# 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
JonRowe marked this conversation as resolved.
Show resolved Hide resolved
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