ruby on rails - Rspec: Testing nested destroy action -


i trying test 'destroy' action nested comments controller. post has_many comments. have ran similar issues before , understand need pass id, i'm still running familiar error...

failures:    1) commentscontroller#delete destroy deletes 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         before :each           @post = factorygirl.create(:post)         end       ....           describe '#delete destroy'           'deletes comment'             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   resources :comments end 

rake routes

☹ rake routes            prefix verb   uri pattern                                 controller#action              root    /                                           posts#index     post_comments    /posts/:post_id/comments(.:format)          comments#index                   post   /posts/:post_id/comments(.:format)          comments#create  new_post_comment    /posts/:post_id/comments/new(.:format)      comments#new edit_post_comment    /posts/:post_id/comments/:id/edit(.:format) comments#edit      post_comment    /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    /posts(.:format)                            posts#index                   post   /posts(.:format)                            posts#create          new_post    /posts/new(.:format)                        posts#new         edit_post    /posts/:id/edit(.:format)                   posts#edit              post    /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 want request this:

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

but sending following parameters:

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

you need send comment.id id parameter.

try this:

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

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -