Rails 在功能测试中找不到路线
Rails 版本 3.0.4 和 Ruby 1.9.2
我正在使用 devise gem,并设置了我的应用程序,以便用户必须登录才能执行任何操作。在编写功能测试时,我发现所有测试都抛出相同的错误。
`method_missing': undefined method `new_user_session_path'
这很奇怪,因为如果我rake paths
我可以看到
new_user_session GET /devise/login(.:format) {:action=>"new", :controller=>"devise/sessions"}
我还可以确认当我运行应用程序时,使用该路径时所有link_to
都可以工作。
对于某个控制器的测试,我需要做一些特殊的事情才能看到其他路线吗?
路线.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
测试/功能/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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要
在测试中添加某个位置,可能是在设置方法中。
请参阅此处的讨论。
You need to add
somewhere in your test, probably in the setup method.
See discussion here.
您可以尝试重新排序路线,使设计路线排在第一位吗?
因此,routes.rb 文件看起来像这样 -
Can you try re-ordering the routes so that the devise routes are first?
So the routes.rb file would look something like this -