如何在Rails中测试RSpec中的204响应?

时间:2012-03-03 08:02:52

标签: ruby-on-rails ruby rspec shoulda

我正在测试资源控制器的删除操作,如下所示:

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

我期待204 /无内容回复。但是,此测试失败,因为服务器返回的响应是406.当我直接从我的Rest测试客户端命中控制器时,响应为204.

2 个答案:

答案 0 :(得分:7)

This页面显示了如何测试响应代码。

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

答案 1 :(得分:5)

几年过去了......

我只想指出可以使用expect语法并直接查询状态代码。

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