Rspec&工厂女孩:没有路线匹配

时间:2015-03-16 16:57:12

标签: ruby-on-rails ruby rspec factory-bot

在使用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 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

1 个答案:

答案 0 :(得分:3)

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

: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])