Rspec & Factory Girl:没有路线匹配

Rspec & Factory Girl: No route matches

在使用 Rspec 学习 TDD 的乏味旅程中,我 运行 在测试控制器为嵌套对象创建操作时出错,评论。 Comments belong_to a Post。我没有运气在 Stack Overflow 上找到答案。这是我目前所拥有的。

测试。

comments_controller_spec.rb

require 'rails_helper'
require 'shoulda-matchers'

RSpec.describe CommentsController, :type => :controller do

        describe '#GET create' do
            before :each do
              @post = FactoryGirl.create(:post)
              post :create, comment: attributes_for(:comment, post_id: @post)
            end

            it 'creates a comment' do
              expect(Comment.count).to eq(1)
            end
        end
end

失败。

Failures:

  1) CommentsController#GET create creates a comment
     Failure/Error: post :create, comment: attributes_for(:comment, post_id: @post)
     ActionController::UrlGenerationError:
       No route matches {:action=>"create", :comment=>{:name=>"MyString", :body=>"MyText", :post_id=>"1"}, :controller=>"comments"}
     # ./spec/controllers/comments_controller_spec.rb:9:in `block (3 levels) in <top (required)>'

comments_controller.rb

class CommentsController < ApplicationController

    def create
        @post = Post.find(params[:id])

        @comment = @post.comments.create(comment_params)
        redirect_to @post
    end


    private

    def comment_params
        params.require(:comment).permit(:name, :body)
    end
end

routes.rb

 root 'posts#index'

 resources :posts do
  resources :comments
end

rake 路线

☹ rake routes
           Prefix Verb   URI Pattern                                 Controller#Action
             root GET    /                                           posts#index
    post_comments GET    /posts/:post_id/comments(.:format)          comments#index
                  POST   /posts/:post_id/comments(.:format)          comments#create
 new_post_comment GET    /posts/:post_id/comments/new(.:format)      comments#new
edit_post_comment GET    /posts/:post_id/comments/:id/edit(.:format) comments#edit
     post_comment GET    /posts/:post_id/comments/:id(.:format)      comments#show
                  PATCH  /posts/:post_id/comments/:id(.:format)      comments#update
                  PUT    /posts/:post_id/comments/:id(.:format)      comments#update
                  DELETE /posts/:post_id/comments/:id(.:format)      comments#destroy
            posts GET    /posts(.:format)                            posts#index
                  POST   /posts(.:format)                            posts#create
         new_post GET    /posts/new(.:format)                        posts#new
        edit_post GET    /posts/:id/edit(.:format)                   posts#edit
             post GET    /posts/:id(.:format)                        posts#show
                  PATCH  /posts/:id(.:format)                        posts#update
                  PUT    /posts/:id(.:format)                        posts#update
                  DELETE /posts/:id(.:format)                        posts#destroy

问题是您发送的参数是这样的:

:comment=>{:name=>"MyString", :body=>"MyText", :post_id=>"1"}

应该是:

:comment=>{:name=>"MyString", :body=>"MyText"}, :post_id=>"1"

这将正确找到路线。

换行:

post :create, comment: attributes_for(:comment, post_id: @post)

至:

post :create, comment: attributes_for(:comment), post_id: @post

您的控制器也需要更改为:

@post = Post.find(params[:post_id])