7

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ivan Santos
  • 626
  • 2
  • 7
  • 19

1 Answers1

7

So far your test are ensuring this:

  • the response of the get request before deleting
  • the status code of the get request
  • the response of the delete request
  • the status code of the delete request
  • the response of the get request after deleting
  • the status code of the get request

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
Paulo Henrique
  • 1,025
  • 8
  • 12
  • I already have all the tests for all the URL and methods. My question is about I just need to test the delete request, than check if the object exist on the database? Or I can test using my way, creating two categories on the database, delete one and list all the categories to check if the deleted one is on the array. Check this link: https://github.com/larica/larica-api/blob/342ca6ff4a6f69651c814515a7be950afb8a47b0/spec/api/v1_categories_spec.rb – Ivan Santos Oct 12 '14 at 01:40
  • Both methods are ok. =) – Paulo Henrique Oct 12 '14 at 02:02
  • It is just a matter of taste. I don't like to mix all routes in the same spec if i can, but as i said, it is just my opinion. Your way is fine! – Paulo Henrique Oct 12 '14 at 02:03
  • Nice, I liked the delete test You made. I just have this question, I know I created a lot of code, but If I can see another ways, I'm not sure if the way is the right one. If we can write code using the "Unit test" philosophy maybe I just need to check if the `Category.find(category.id)` returns an error or not. Something like that. That's something I like to have more feedbacks about. – Ivan Santos Oct 12 '14 at 02:07
  • In the end, all that matters is if you are really making sure it is working and if you can sleep at night without worries. =) – Paulo Henrique Oct 12 '14 at 02:11
  • The good writing and reading is a plus that we are always targeting but not something to stop your good nights of sleeping. Just search for a balance between the good reading/writing and the certainty of a good code. =) – Paulo Henrique Oct 12 '14 at 02:13
  • Is something missing on my answer? Or maybe there is another answer you were expecting? – Paulo Henrique Oct 14 '14 at 19:39
  • 1
    I think your answer looks nice. I just need more perspectives about that. I didn't find a "style guide" for that. Thanks dude! o/ – Ivan Santos Oct 15 '14 at 01:33