diff --git a/NEWS.md b/NEWS.md index d2bb130e0..68e8ed472 100644 --- a/NEWS.md +++ b/NEWS.md @@ -34,6 +34,10 @@ * Fix `validate_inclusion_of` used with `with_message` so that it fails if given a message that does not match the message on the validation. ([#598]) +* Fix `route` matcher so that when controller and action are specified in hash + notation (e.g. `posts#show`), route parameters such as `id` do not need to be + specified as a string but may be specified as a number as well ([#602]). + ### Features * Add ability to test `:primary_key` option on associations. ([#597]) @@ -51,6 +55,7 @@ [#597]: https://github.com/thoughtbot/shoulda-matchers/pull/597 [#537]: https://github.com/thoughtbot/shoulda-matchers/pull/537 [#598]: https://github.com/thoughtbot/shoulda-matchers/pull/598 +[#602]: https://github.com/thoughtbot/shoulda-matchers/pull/602 # 2.7.0 diff --git a/lib/shoulda/matchers/action_controller/route_params.rb b/lib/shoulda/matchers/action_controller/route_params.rb index 8d7afb969..a1cd5e65e 100644 --- a/lib/shoulda/matchers/action_controller/route_params.rb +++ b/lib/shoulda/matchers/action_controller/route_params.rb @@ -24,14 +24,19 @@ def controller_and_action_given_as_string? end def extract_params_from_string - params = args[1] || {} controller, action = args[0].split('#') - params.merge!(controller: controller, action: action) + params = (args[1] || {}).merge(controller: controller, action: action) + stringify_values(params) end def stringify_params - args[0].each do |key, value| - args[0][key] = stringify(value) + stringify_values(args[0]) + end + + def stringify_values(hash) + hash.inject({}) do |hash_copy, (key, value)| + hash_copy[key] = stringify(value) + hash_copy end end diff --git a/spec/unit/shoulda/matchers/action_controller/route_matcher_spec.rb b/spec/unit/shoulda/matchers/action_controller/route_matcher_spec.rb index f492f2299..fa0b6eb51 100644 --- a/spec/unit/shoulda/matchers/action_controller/route_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/action_controller/route_matcher_spec.rb @@ -56,6 +56,14 @@ to route(:get, "/#{controller_name}"). to("#{controller_name}#index") end + + context 'when route has parameters' do + it 'accepts a non-string parameter' do + expect(controller_with_defined_routes). + to route(:get, "/#{controller_name}/1"). + to("#{controller_name}#show", id: 1) + end + end end def controller_with_defined_routes