I started using that way:
describe "DELETE /v1/categories/{id}" do
before(:each) do
# Login User/Token
end
it 'deletes a category' do
category = Fabricate(:category)
category2 = Fabricate(:category)
get "/v1/categories"
expect(response.status).to eq 200
expect(JSON.parse(response.body)).to eq([YAML.load(category.to_json),YAML.load(category2.to_json),])
delete "/v1/categories/#{category.id}"
expect(response.status).to eq 200
get "/v1/categories"
expect(JSON.parse(response.body)).to eq([YAML.load(category2.to_json)])
end
end
I'm not sure if is the best way to test an API Request to delete data.
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 unit test framework for the Ruby programming language. RSpec is different than traditional xUnit frameworks like JUnit because RSpec is a Behavior driven development tool.
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.
So far your test are ensuring this:
This test is covering a lot more then the delete request but i think it is fine. Its better to have this kind of tests then having none.
What i wold do to improve this test would be to split the routes when testing. I would have 1 test to ensure the index route is working as expected and 1 test to make sure the delete route is working. This way a bug on the index route won't break your delete spec. =)
I would have something like this:
describe "GET /v1/categories" do
before(:each) do
# Login User/Token
category = Fabricate(:category)
category2 = Fabricate(:category)
get "/v1/categories"
end
it 'should return status 200' do
expect(response.status).to eq 200
end
it 'list all categories' do
expect(JSON.parse(response.body)).to eq([YAML.load(category.to_json),YAML.load(category2.to_json),])
end
end
describe "DELETE /v1/categories/:category_id" do
before(:each) do
# Login User/Token
category = Fabricate(:category)
category2 = Fabricate(:category)
delete "/v1/categories/#{category.id}"
end
it 'should return status 200' do
expect(response.status).to eq 200
end
it 'should delete the category' do
expect(Category.all).to eq category2
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