具有旅程1.0.4的单元测试导致ActionController :: RoutingError:没有路由匹配

时间:2013-01-19 13:41:37

标签: ruby-on-rails unit-testing ruby-on-rails-3.2 rails-routing journey

SO-thread Routing error when updating to Rails 3.2.6 or Rspec 2.11.0很好地解释了这个问题:

使用 journey> = 1.0.4(Rails 3.2.11),所有与路由相关的单元测试都会失败,而应用程序本身仍可正常运行。

使用 Rails 3.2.6 并锁定1.0.3之旅是一种变得无法接受的解决方法。

举个例子,这是最简单的失败测试之一:

的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

栈跟踪

  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>'

在SO上似乎有一些相关的问题 - 无论是未解决的还是没有特定的解决方案。

任何人都知道如何解决此问题?

1 个答案:

答案 0 :(得分:0)

rails-translate-routes README(https://github.com/francesc/rails-translate-routes#testing)的“测试”部分解释了必须将语言环境作为参数添加到动作调用中。

get :new
必须将

更改为以下内容:

get :new, locale: 'en'

如自述文件中所述,存在猴子补丁以避免必须这样做。

仍然存在一个问题:为什么没有在Rails 3.2.6 / Journey 1.0.3中添加它?