Rspec:测试嵌套销毁操作 [英] Rspec: Testing nested destroy action

查看:16
本文介绍了Rspec:测试嵌套销毁操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试嵌套评论控制器的销毁"操作.发表有_许多评论.我之前也遇到过类似的问题,我知道我需要传递一个 id,但我仍然遇到了一个非常熟悉的错误......

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 need to pass an id, but I'm still running into a very familiar error...

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

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

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

    @comment.destroy
    redirect_to post_path(@post)
end

routes.rb

 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

推荐答案

你要请求的路由是这样的:

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"}

您需要将 comment.id 作为 id 参数发送.

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

试试这个:

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

这篇关于Rspec:测试嵌套销毁操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆