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.
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.
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.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With