ruby-on-rails - Rails 功能测试、路由错误

标签 ruby-on-rails testing routes functional-testing

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

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

现在我尝试在消息 Controller 中运行一个简单的测试:

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

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

完整的routes.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。我不知道为什么。

最佳答案

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

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

如果您所做的事情基本相同,那么路由问题可能是由路由文件中的冲突引起的,我怀疑可能就是这种情况。你可以发布吗?仅供引用,我写了一个article on testing your routes ,这将是一个非常好的主意,因为它可以在路由错误干扰 Controller 之前尽早捕获它们。

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

更新:查看您的路线后,发现存在一些冲突。您可以将消息作为多个其他资源的子资源,但在消息 Controller 中,您必须考虑 params[:me_id] 或 params[:profile_id] 的可能性。看起来它们实际上都是底层的用户模型,所以它可以很简单:

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

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

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

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

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

关于ruby-on-rails - Rails 功能测试、路由错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2154944/

相关文章:

ruby-on-rails - 在 Ruby on Rails 中创建对象列表或数组

ruby-on-rails - heroku & devise 用户注册注册 Controller 错误

javascript - Cypress getByTestId、queryByTestId、findByTestId 检查元素是否不存在

sql - 从数据库中提取行,包括依赖行

php - Controller 和路由 - Laravel 8

ruby-on-rails - 无法通过在 Rails 应用程序中运行 Ruby 打开本地主机

ruby-on-rails - 如何通过file_field标签限制只能上传图片?

node.js - 测试 findOneAndUpdate upsert 重复预防

Flutter:Autoroute:RouteGuard 在 AutoTabsScaffold 中不工作

php - Symfony 4 - 路由 : "The requested URL was not found on this server"