Coffee Bite
Coffee Bite

Reputation: 5164

How do I test for a 204 response in RSpec in Rails?

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.

Upvotes: 11

Views: 5535

Answers (2)

hfhc2
hfhc2

Reputation: 4391

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

Upvotes: 9

Gazler
Gazler

Reputation: 84150

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

Upvotes: 7

Related Questions