11

I am testing the delete action of my resource controller as follows:

describe ResourceController do
  context "DELETE destroy" do
    before :each do
      delete :destroy, id: @resource.id
    end
    it { should respond_with(:no_content) }
  end
end

I expect a 204/no-content response. However, this test is failing as the response returned by the server is a 406. The response is a 204 when I hit the controller directly from my Rest test client.

Coffee Bite
  • 4,956
  • 5
  • 33
  • 38

2 Answers2

9

A couple of years have passed...

I would just like to note that it is possible to use the expect syntax and to query the status code directly.

describe ResourceController do
  context "DELETE destroy" do
    it "should respond with a 204"
      delete :destroy, id: @resource.id
      expect(response).to have_http_status(:no_content)
    end
  end
end
hfhc2
  • 4,182
  • 2
  • 27
  • 56
7

This page shows how to test the response code.

describe ResourceController do
  context "DELETE destroy" do
    it "should respond with a 204"
      delete :destroy, id: @resource.id
      response.code.should eql(204)
    end
  end
end
Gazler
  • 83,029
  • 18
  • 279
  • 245
  • the syntax is fine. I forgot to mention that I am using shoulda. I wonder if some headers need to be set when making the delete request. – Coffee Bite Mar 04 '12 at 05:39
  • Please post the contents of your controller. – Gazler Mar 04 '12 at 11:13
  • 1
    Note: In Rails 4 at least, `response.code` is a string, so you should use `response.code.should eql "204"` or `response.response_code.should eql 204`. See http://api.rubyonrails.org/classes/ActionDispatch/Response.html – Soup Aug 30 '13 at 12:51