Skip to content

functional tests RoutingError when route exists #4401

Closed
kenmazaika opened this Issue Jan 10, 2012 · 6 comments

6 participants

@kenmazaika

I was looking to add a parameter that should be present base on the route. This works in a controller and when viewing the page, but when writing a functional test it causes the route to never be found.

resources :awesomes, :awesome_level => 100, :only => [:index]

causes the awesomes controller index action to have param[:awesome_level] set to 100, but now I cannot write a functional test for this action. When I write a functional test it causes:

ActionController::RoutingError: No route matches {:controller=>"awesomes"}

To happen when I do not believe it should. If the :awesome_level is removed from the routes file, the test passes.

Example of the bug is in a bare rails project, with only the problem located at the github repo here:

https://github.com/kenmazaika/routing-error-rails

@kenmazaika

I should note that when you go to:

http://localhost:4567/awesomes

the following is rendered:

{"awesome_level":100}

@tenderlove tenderlove was assigned Jan 11, 2012
@isaacsanders

Is this still an issue?

@steveklabnik
Ruby on Rails member

The test app is on Rails 3.0.10. I updated it to 3.2.8, and the problem still persists.

I noticed the app was using JRuby. I updated the Gemfile to include sqlite3 and used MRI, and I get the same failure, with this backtrace:

  1) Error:
test_awesomes_index(AwesomesControllerTest):
ActionController::RoutingError: No route matches {:controller=>"awesomes"}
    /Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:532:in `raise_routing_error'
    /Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:528:in `rescue in generate'
    /Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:520:in `generate'
    /Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:561:in `generate'
    /Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:557:in `generate_extras'
    /Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:553:in `extra_keys'
    /Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/test_case.rb:147:in `assign_parameters'
    /Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/test_case.rb:459:in `process'
    /Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/test_case.rb:49:in `process'
    /Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/test_case.rb:386:in `get'
    /Users/steve/tmp/routing-error-rails/test/functional/awesomes_controller_test.rb:5:in `block in <class:AwesomesControllerTest>'

@robertjwhitney

Same on 3.2.12

ActionDispatch::Routing::RoutSet::Generator#generate is throwing Journey::Router::RoutingError when trying to execute:

  path, params = @set.formatter.generate(:path_info, named_route, options, recall, PARAMETERIZE)
  # ...

Which is being rescued:

# lib/action_dispatch/routing/route_set.rb:520
def generate
  path, params = @set.formatter.generate(:path_info, named_route, options, recall, PARAMETERIZE)

  raise_routing_error unless path

  return [path, params.keys] if @extras

  [path, params]
rescue Journey::Router::RoutingError
  raise_routing_error
end

but after that I found I was chasing it all over the place. Could probably use some guidance on where to look next.

@robertjwhitney

chased it to rails/actionpack/lib/action_controller/test_case.rb:151

def assign_parameters(routes, controller_path, action, parameters = {})
  # ...
  extra_keys = routes.extra_keys(parameters)
  # ...
@pixeltrix
Ruby on Rails member

This is fixed by f1d8f2a in Rails 4.

You also need to make it an explicit default - unknown options are treated as constraints, e.g:

resources :awesomes, only: [:index], defaults: { awesome_level: 100 }
@pixeltrix pixeltrix closed this Apr 22, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.