ruby-on-railsunit-testingruby-on-rails-3.2rails-routingjourney

Unit tests with journey 1.0.4 result in ActionController::RoutingError: No route matches


The SO-thread Routing error when updating to Rails 3.2.6 or Rspec 2.11.0 explains the problem pretty well:

With journey >= 1.0.4 (Rails 3.2.11), all routing-related unit tests fail, while the app itself continues to function just fine.

Using Rails 3.2.6 and locking journey to 1.0.3 is a workaround that has become unacceptable.

As an example, here is one of the simplest tests that fail:

routes.rb

ProjRails::Application.routes.draw do

    devise_for :users, :controllers => {:registrations => "users/registrations"}

    resources :users do
      collection do
        get 'current' => 'users#redirect', :as => :current
      end
    end

    # ...

  end
end

users_controller_test.rb

require 'test_helper'

class UsersControllerTest < ActionController::TestCase
  setup do
    @user = FactoryGirl.create(:user)
    @current_user = FactoryGirl.create(:user)
    sign_in @current_user
  end

  # ...

  test "should get new" do
    get :new                    # this is line 25
    assert_response :success
  end

  # ...

end

stacktrace

  8) Error:
test_should_get_new(UsersControllerTest):
ActionController::RoutingError: No route matches {:controller=>"users", :action=>"new"}
    /home/me/.rvm/gems/ruby-1.9.3-p362/gems/actionpack-3.2.11/lib/action_dispatch/routing/route_set.rb:533:in `raise_routing_error'
    /home/me/.rvm/gems/ruby-1.9.3-p362/gems/actionpack-3.2.11/lib/action_dispatch/routing/route_set.rb:529:in `rescue in generate'
    /home/me/.rvm/gems/ruby-1.9.3-p362/gems/actionpack-3.2.11/lib/action_dispatch/routing/route_set.rb:521:in `generate'
    /home/me/.rvm/gems/ruby-1.9.3-p362/gems/actionpack-3.2.11/lib/action_dispatch/routing/route_set.rb:562:in `generate'
    /home/me/.rvm/gems/ruby-1.9.3-p362/gems/actionpack-3.2.11/lib/action_dispatch/routing/route_set.rb:558:in `generate_extras'
    /home/me/.rvm/gems/ruby-1.9.3-p362/gems/actionpack-3.2.11/lib/action_dispatch/routing/route_set.rb:554:in `extra_keys'
    /home/me/.rvm/gems/ruby-1.9.3-p362/gems/actionpack-3.2.11/lib/action_controller/test_case.rb:151:in `assign_parameters'
    /home/me/.rvm/gems/ruby-1.9.3-p362/gems/actionpack-3.2.11/lib/action_controller/test_case.rb:463:in `process'
    /home/me/.rvm/gems/ruby-1.9.3-p362/gems/actionpack-3.2.11/lib/action_controller/test_case.rb:49:in `process'
    /home/me/.rvm/gems/ruby-1.9.3-p362/gems/devise-2.1.2/lib/devise/test_helpers.rb:19:in `block in process'
    /home/me/.rvm/gems/ruby-1.9.3-p362/gems/devise-2.1.2/lib/devise/test_helpers.rb:71:in `catch'
    /home/me/.rvm/gems/ruby-1.9.3-p362/gems/devise-2.1.2/lib/devise/test_helpers.rb:71:in `_catch_warden'
    /home/me/.rvm/gems/ruby-1.9.3-p362/gems/devise-2.1.2/lib/devise/test_helpers.rb:19:in `process'
    /home/me/.rvm/gems/ruby-1.9.3-p362/gems/actionpack-3.2.11/lib/action_controller/test_case.rb:390:in `get'
    /home/me/proj/test/functional/users_controller_test.rb:25:in `block in <class:UsersControllerTest>'

There seem to be quite some related questions on SO - all either unresolved or without a specific solution.

Anyone knows how to resolve this issue?


Solution

  • The section "Testing" of the rails-translate-routes README (https://github.com/francesc/rails-translate-routes#testing) explains that the locale has to be added as a parameter to the action call.

    get :new
    

    has to be changed to something like this:

    get :new, locale: 'en'
    

    As outlined in the readme, a monkey patch exists to avoid the necessity of doing so.

    One question still remains: Why did it work without that addition in Rails 3.2.6 / Journey 1.0.3?