Rails在功能测试中找不到路线 [英] Rails Can't find route in Functional Test

查看:113
本文介绍了Rails在功能测试中找不到路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rails版本3.0.4和Ruby 1.9.2

Rails version 3.0.4 and Ruby 1.9.2

我正在使用devise gem,并设置了我的应用程序,以便用户必须登录执行任何操作。在编写功能测试时,我发现所有测试都引发了相同的错误。

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"}

我还可以确认在运行应用程序时,使用该路径时,所有 link_to 的工作。

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

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

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


推荐答案

您需要添加

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

在测试中的某个地方,可能在设置方法。

somewhere in your test, probably in the setup method.

请参见此处的讨论

这篇关于Rails在功能测试中找不到路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆