简体   繁体   English

Rspec:测试嵌套销毁动作

[英]Rspec: Testing nested destroy action

I am trying to test the 'destroy' action for my nested comments controller. 我正在尝试为嵌套的注释控制器测试“破坏”操作。 Post has_many Comments . Post has_many Comments I have ran into similar issues before and understand that I need to pass an id, but I'm still running into a very familiar error... 我以前也遇到过类似的问题,并且了解我需要传递一个id,但是我仍然遇到一个非常熟悉的错误...

Failures:

  1) CommentsController#DELETE destroy deletes a comment
     Failure/Error: delete :destroy, comment: create(:comment), post_id: @post
     ActionController::UrlGenerationError:
       No route matches {:action=>"destroy", :comment=>"1", :controller=>"comments", :post_id=>"1"}
     # ./spec/controllers/comments_controller_spec.rb:19:in `block (3 levels) in <top (required)>'

comments_controller_spec.rb comments_controller_spec.rb

RSpec.describe CommentsController, :type => :controller do
        before :each do
          @post = FactoryGirl.create(:post)
        end


    ....


        describe '#DELETE destroy' do
          it 'deletes a comment' do
            delete :destroy, comment: create(:comment), post_id: @post
            expect(response).to redirect_to post_path
          end
        end
end

comments_controller.rb comments_controller.rb

def destroy
    @post = Post.find(params[:post_id])
    @comment = @post.comments.find(params[:id])

    @comment.destroy
    redirect_to post_path(@post)
end

routes.rb routes.rb

 resources :posts do
  resources :comments
end

rake routes 耙路

☹ 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

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. 您需要发送comment.id作为id参数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM