shoulda-matchers: Using the route matcher with 'format' can lead to failing specs, while everything is correct

When having a route, or namespace like this:

namespace :api, defaults: { format: 'json' } do
  # some routes
end

and trying to assert this behavior using the route shoulda-matcher

it { is_expected.to route(:get, '/api/projects/1').to(action: :show, id: 1, format: :json) }

the spec will fail with something like:

The recognized options <{"controller"=>"projects", "action"=>"show", "id"=>"1", "format"=>"json"}> did not match <{"action"=>"show", "id"=>"1", "format"=>:json, "controller"=>"projects"}>, difference:.
--- expected
+++ actual
@@ -1 +1 @@
-{"action"=>"show", "id"=>"1", "format"=>:json, "controller"=>"projects"}
+{"controller"=>"projects", "action"=>"show", "id"=>"1", "format"=>"json"}

because the matcher will try to match the symbol (spec) against a string (route.rb).

See https://github.com/thoughtbot/shoulda-matchers/pull/693 for more details.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 3
  • Comments: 24

Commits related to this issue

Most upvoted comments

For those waiting for fix - just put this

Shoulda::Matchers::ActionController::RouteParams::PARAMS_TO_SYMBOLIZE = []

into spec_helper.rb. That worked for me.