Rails Can't find route in Functional Test

こ雲淡風輕ζ 提交于 2019-12-24 08:51:30

问题


Rails version 3.0.4 and Ruby 1.9.2

I'm using the devise gem, and have my application set up so that a user must sign in to perform any action. While writing my functional tests, I found that all of the test threw the same error.

`method_missing': undefined method `new_user_session_path'

This is strange as, if I rake routes I can see

new_user_session GET    /devise/login(.:format)       {:action=>"new", :controller=>"devise/sessions"}

I can also confirm that when I run the application, all of the link_tos work, when using that path.

Is there something special I have to do for a certain controller's test to see other routes?

routes.rb

Nge::Application.routes.draw do
  resources :companies
  resources :users
  devise_for :users, :path => "devise", :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification', :unlock => 'unblock', :registration => 'register', :sign_up => 'cmon_let_me_in' }
...
end

test/functional/companies_controller_test.rb

require 'test_helper'
class CompaniesControllerTest < ActionController::TestCase
  context "When a user is NOT signed in" do
    [:index, :new].each do |action|
      context "and GET ##{action.to_s}" do
        setup {get action}
        should set_the_flash.to(/must sign-up/)
        should redirect_to(new_user_session_path)
      end
    end
  ...
  end
end

回答1:


You need to add

@request.env["devise.mapping"] = Devise.mappings[:user]

somewhere in your test, probably in the setup method.

See discussion here.




回答2:


Can you try re-ordering the routes so that the devise routes are first?

So the routes.rb file would look something like this -


Nge::Application.routes.draw do
  resources :companies
  devise_for :users, :path => "devise", :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification', :unlock => 'unblock', :registration => 'register', :sign_up => 'cmon_let_me_in' }
  resources :users
...
end



来源:https://stackoverflow.com/questions/5143750/rails-cant-find-route-in-functional-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!