【问题标题】:"no routes match" when running automatically generated functional tests运行自动生成的功能测试时出现“无路由匹配”
【发布时间】:2014-06-08 18:11:22
【问题描述】:

我正在尝试解决以下错误:

ElementsControllerTest#test_should_get_create:
ActionController::UrlGenerationError: No route matches {:action=>"create", :controller=>"elements"}
test/controllers/elements_controller_test.rb:15:in `block in <class:ElementsControllerTest>'

Element 是一个嵌套资源(Piece 的子级)

    resources :batiments do
      resources :pieces
    end

    resources :pieces, shallow: true do
      resources :elements
    end

test/controllers/elements_controller.rb

class ElementsController < ApplicationController
 #after_filter :load_piece

  def index
   @piece = Piece.find(params[:piece_id])
    @elements = @piece.elements.all
  end

  def new
    @piece = Piece.find(params[:piece_id])
    @element = @piece.elements.new
  end


 def create
   @piece = Piece.find(params[:piece_id])
   @element = @piece.elements.new(element_params)
   if @element.save
     flash[:notice] = "Elément créé!"
   else
     flash[:alert] = "Elément non créé!"
   end
   redirect_to piece_path(@piece)
 end

…

end

还有我的 Element 路线:

            Prefix Verb   URI Pattern                                       Controller#Action

     piece_elements GET    /pieces/:piece_id/elements(.:format)              elements#index
                    POST   /pieces/:piece_id/elements(.:format)              elements#create
  new_piece_element GET    /pieces/:piece_id/elements/new(.:format)          elements#new
       edit_element GET    /elements/:id/edit(.:format)                      elements#edit
            element GET    /elements/:id(.:format)                           elements#show
                    PATCH  /elements/:id(.:format)                           elements#update
                    PUT    /elements/:id(.:format)                           elements#update
                    DELETE /elements/:id(.:format)                           elements#destroy
             pieces GET    /pieces(.:format)                                 pieces#index
                    POST   /pieces(.:format)                                 pieces#create
          new_piece GET    /pieces/new(.:format)                             pieces#new
         edit_piece GET    /pieces/:id/edit(.:format)                        pieces#edit
              piece GET    /pieces/:id(.:format)                             pieces#show
                    PATCH  /pieces/:id(.:format)                             pieces#update
                    PUT    /pieces/:id(.:format)                             pieces#update
                    DELETE /pieces/:id(.:format)                             pieces#destroy

更新:elements_controller_test.rb 在 /test

     require 'test_helper'

    class ElementsControllerTest < ActionController::TestCase
      test "should get create" do
        get :create
        assert_response :success
      end
    end

【问题讨论】:

  • 请在您的问题中分享elements_controller_test.rb
  • 谢谢柯蒂。完成了。

标签: ruby-on-rails unit-testing ruby-on-rails-4 routes nested-resources


【解决方案1】:

如下改变你的测试用例:

  class ElementsControllerTest < ActionController::TestCase
      test "should get create" do
        post :create, element: {attrb1: "value", attrb2: "value"}, piece_id: id_of_existing_piece_record                    ## Post method
        assert_response :success
      end
    end

createpost 路由而不是 get 路由。这就是为什么您收到错误为No route matches {:action=&gt;"create", :controller=&gt;"elements"} 此外,由于 elements 嵌套在 pieces 中,您必须将现有片段的 id 传递给您的 post :create 调用。

element: {attrb1: "value", attrb2: "value"} 用于您要保存在元素记录中的属性。

【讨论】:

  • 感谢您的回答,但这并不能解决错误。我补充说 Rails 生成了这些单元测试,而不是我......
  • 由于元素嵌套在片段中,您必须将现有片段的 id 传递给您的 post :create 调用。
  • @Dwidoo 查看我的更新答案以获得更好的解释
  • 我尝试了您的解决方案,但得到了 "ActiveRecord::RecordNotFound: Couldn't find Piece with id=1" 。我已经用“Piece.find(1)”检查了 Rails 控制台,它确实存在。我错过了什么吗?我的创建方法有问题吗?
  • test环境中是否存在?运行rails console -e test,然后运行Piece.find(1)。告诉我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-07
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多