问题
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_to
s 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