【问题标题】:Rails functional tests, routing errorRails 功能测试,路由错误
【发布时间】:2010-01-28 13:55:27
【问题描述】:

我是测试新手,在尝试运行功能测试时遇到了一些困难。

我在这里有一个 messages_controller 和一个 user_controller。 在路由中,我定义了用户资源 has_many 消息资源。

现在我正在尝试在消息控制器中运行一个简单的测试:

def test_index
  get :index, { :user_id => 1 }
  assert_template 'index'
end

但是从 rails 得到一个路由错误,他找不到消息的路由。我不想仅仅因为测试而包含消息路由。如何告诉测试他必须从 /users/messages url 访问?

完整的路线.rb:

ActionController::Routing::Routes.draw do |map|

  map.login  'login',   :controller => :user_sessions, :action => :new
  map.logout 'logout',  :controller => :user_sessions, :action => :destroy
  map.signin 'signin',  :controller => :users,         :action => :new

  map.connect 'search/:action/:word', :controller => :search
  map.connect 'search/:word',         :controller => :search, :action => :index

  map.resources :forums do |forums| 
    forums.resources :forum_posts, :collection => {:preview => :post }, :as => :posts do |post|
      post.resources :forum_posts, :as => :reply
      post.resources :reports
    end
  end

  map.resources :newsitems, :as => :news do |news|
    news.resources :comments do |comment|
      comment.resources :reports
    end
  end

  map.resource :user_sessions
  map.resources :users, 
                :as => :profiles,
                :controller => :profiles,
                :has_many   => [ :messages ]
  map.resource :profiles
  map.resource :me,            
               :controller => :me,
               :has_many   => [ :messages ]


  map.resources :comments, :has_many => [ :reports ]
  map.resources :forum_posts, :has_many => [ :reports ]
  map.resources :reports

  map.home  '/', :controller => :home
  map.root  :controller => :home

  map.namespace :admin do |admin|
    admin.namespace :forum do |forum|
      forum.resources :categories
      forum.resources :posts
      forum.resources :forums
      forum.root      :controller => :home
    end
    admin.resources :notices
    admin.resources :users
    admin.workflow  'workflow/:action', :controller => :workflow
    admin.resources :newsitems
    admin.resources :reports
    admin.resources :comments    
    admin.root :controller => :home
  end

  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
end

更新

我注意到每个功能测试都会出现路由错误。即使是最简单的,比如newsitem。我不知道为什么。

【问题讨论】:

    标签: ruby-on-rails testing routing functional-testing


    【解决方案1】:

    我在一个空白的 Rails 应用程序中使用您指定的路由代码重新创建了您的场景,并测试了您指定的,并且它没有问题地工作,正如它应该的那样。我将在此处粘贴我的控制器代码,因为这是您唯一遗漏的部分:

    class MessagesController < ApplicationController
      def index
        @messages = User.find(params[:user_id]).messages
      end
    end
    

    如果您的做法基本相同,那么路由问题可能是由您的路由文件中的冲突引起的,我怀疑可能是这种情况。你能发帖吗?仅供参考,我写了一个article on testing your routes,这是一个非常好的主意,因为它会在路由错误干扰控制器之前尽早发现它们。

    无论如何,如果您可以发布您的路线,我可以看看。

    更新:查看您的路线后,有一些冲突。您可以将消息作为多个其他资源的子资源,但在您的消息控制器中,您将不得不考虑 params[:me_id] 或 params[:profile_id] 的可能性。看起来他们都是真正的用户模型,所以它可以很简单:

    @user = User.find(params[:me_id] || params[:profile_id])
    

    您可能希望将其抽象为您使用before_filter 调用的方法。

    另一个问题是您有两条重叠的配置文件路线,我不知道为什么。我不认为这是测试中的路由错误,因为无论如何测试都会绕过路由引擎。我认为这是索引视图中的错误,因为它可能包含指向格式不正确的消息的链接。例如,如果您有一条消息的链接,并且您有一个 @profile 对象,那么您需要像这样调用它们:

    <%= link_to message.name, profile_message_path(@profile, @message) %>
    

    但是,如果您使用像 message_path(@message) 这样的非嵌套路径,它将失败,因为没有非嵌套的消息路由。

    【讨论】:

    • 这是我对用户/消息的规则:map.resources :users, :as => :profiles, :controller => :profiles, :has_many => [ :messages ] 它应该仍然有效吗?因为它不是
    • 感谢您的帮助!我添加了完整的 routes.rb。是否可能与消息存在路由冲突?因为用户和 me 控制器有它?
    • 这里还是不行。但我注意到每个功能测试都会出现路由错误。我不知道为什么。
    【解决方案2】:

    这是我的“旅程”宝石中的一个问题。他们在旅程 1.0.4 中使路线更加严格,仅出现在“测试”环境中。有利于“开发”和“生产”。

    ** 确保您使用的参数与路由中声明的参数完全相同 **

    要么添加:

    get :index, :locale => "en"
    

    或在您的 Gemfile 更新中:

    gem 'journey', '1.0.3'
    

    第二种解决方案是暂时的解决方法。理想情况下,您应该测试您的路线将所有精确的参数。 Journey 1.0.4 更加严格

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多