thoughtbot / shoulda-matchers

Simple one-liner tests for common Rails functionality
https://matchers.shoulda.io
MIT License
3.51k stars 912 forks source link

`route` throws TypeError instead of Minitest::Assertion #1489

Open josemigallas opened 2 years ago

josemigallas commented 2 years ago

I'm upgrading shoulda-matchers to 4.5.1 and the following test:

should route(:get, '/stats/api/applications/42/usage').to(id: '42')

returns the following output:

Error:
Stats::Api::ApplicationsControllerTest#test_: Stats::Api::ApplicationsController should route GET /stats/api/applications/42/usage to/from {:id=>"42", :action=>"usage", :format=>:json}. :
TypeError: class or module required for rescue clause

After some debugging it seems that route_recognized? is throwing the assertion error but not rescuing properly:

From: /Users/jgallaso/projects/3scale/porta/vendor/bundle/ruby/2.6.0/gems/shoulda-matchers-4.5.1/lib/shoulda/matchers/action_controller/route_matcher.rb:193 Shoulda::Matchers::ActionController::RouteMatcher#route_recognized?:

    191: def route_recognized?
    192:   binding.pry
 => 193:   context.send(
    194:     :assert_routing,
    195:     { method: method, path: path },
    196:     params,
    197:   )
    198:   true
    199: rescue ::ActionController::RoutingError => e
    200:   @failure_message = e.message
    201:   false
    202: rescue Shoulda::Matchers.assertion_exception_class => e
    203:   @failure_message = e.message
    204:   false
    205: end

[3] pry(#<Shoulda::Matchers::ActionController::RouteMatcher>)> context.send(
[3] pry(#<Shoulda::Matchers::ActionController::RouteMatcher>)* :assert_routing, { method: method, path:path }, params)   
Minitest::Assertion: The recognized options <{"controller"=>"stats/api/applications", "action"=>"usage", "application_id"=>"42", "format"=>"json"}> did not match <{"id"=>"42", "controller"=>"stats/api/applications"}>, difference:.
--- expected
+++ actual
@@ -1 +1 @@
-{"id"=>"42", "controller"=>"stats/api/applications"}
+{"controller"=>"stats/api/applications", "action"=>"usage", "application_id"=>"42" }

Is this something already known? If so, I would gladly submit a PR to fix it.

mcmire commented 2 years ago

Most people use shoulda-matchers with RSpec these days, so if there's an issue with Minitest then it's possible that there's a bug that we haven't patched yet. Feel free to submit a PR!