Rails 在功能测试中找不到路线

发布于 2024-10-19 11:14:02 字数 1303 浏览 2 评论 0原文

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玻璃人 2024-10-26 11:14:02

您需要

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

在测试中添加某个位置,可能是在设置方法中。

请参阅此处的讨论。

You need to add

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

somewhere in your test, probably in the setup method.

See discussion here.

梓梦 2024-10-26 11:14:02

您可以尝试重新排序路线,使设计路线排在第一位吗?

因此,routes.rb 文件看起来像这样 -


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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文