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

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

问题描述

Rails 3.0.4 版和 Ruby 1.9.2

Rails version 3.0.4 and Ruby 1.9.2

我正在使用设计 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'

这很奇怪,如果我rake routes我可以看到

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

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天全站免登陆