Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 577
Coffee Bite Avatar asked Mar 03 '12 08:03

Coffee Bite


People also ask

How do I test an RSpec file?

To run a single Rspec test file, you can do: rspec spec/models/your_spec. rb to run the tests in the your_spec. rb file.

What is an RSpec test?

RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly.

What does RSpec describe do?

The word describe is an RSpec keyword. It is used to define an “Example Group”. You can think of an “Example Group” as a collection of tests. The describe keyword can take a class name and/or string argument.

What is RSpec rails gem?

RSpec is a testing framework written in Ruby to test Ruby code. To get started using RSpec with Rails, add it to the Gemfile. group :development, :test do gem 'rspec-rails' end. Next finish setting it up by running bundle install in your project directory and then. rails generate rspec:install.


2 Answers

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
like image 132
hfhc2 Avatar answered Oct 08 '22 08:10

hfhc2


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
like image 27
Gazler Avatar answered Oct 08 '22 09:10

Gazler