Rspec: Testing nested destroy action

前端 未结 1 773
走了就别回头了
走了就别回头了 2021-01-24 00:02

I am trying to test the \'destroy\' action for my nested comments controller. Post has_many Comments. I have ran into similar issues before and understand that I ne

1条回答
  •  梦毁少年i
    2021-01-24 00:46

    The route that you want to request is this:

    /posts/:post_id/comments/:id(.:format)

    But you are sending the following parameters:

    {:action=>"destroy", :comment=>"1", :controller=>"comments", :post_id=>"1"}

    You need to send the comment.id as the id parameter.

    Try this:

    describe '#DELETE destroy' do
      it 'deletes a comment' do
        comment = create(:comment)
        @post.comments << comment
        # Also, test if the action really deletes a comment.
        expect{delete :destroy, id: comment.id, post_id: @post}.
        to change{@post.comments.count}.by(-1)
        expect(response).to redirect_to post_path
      end
    end
    

    0 讨论(0)
提交回复
热议问题