I’m using respond_to and respond_with pair in Rails 3 to respond to a DELETE #destroy request. The destroy action is defined in UsersController. It destroy the User specified by params[:id] and respond with a JS template. However, the request keeps failing in RSpec test and I’m not really sure how to fix it.
Here’s a snippet for my UsersController:
class UsersController < ApplicationController
respond_to :html, only: [:index, :show, :new, :edit]
respond_to :js, only: [:create, :update, :delete]
...
def destroy
@user = User.find(params[:id])
@user.destroy
respond_with @user
end
end
And here is the test that keeps failing:
require 'spec_helper'
describe UsersController do
...
describe "DELETE #destroy" do
before { @user = create(:user); delete :destroy, id: @user.id, format: :js }
it "return HTTP success" do
response.should be_success
end
it "respond with JS content" do
response.content_type.should == 'text/javascript'
end
end
end
The test “respond with JS content” does succeed, however the test “return HTTP successs” fails. When I use a debugger to check response.code, it is 406. I’m expecting 2xx since the deletion succeed. Is this a normal Rails behavior or do I have something wrong with my code?
Seems you messed up REST
deleteand controller’sdestroy. Rails responder knows nothing about :delete action, changeto
or pass a block to the responder, instead of
use