nodejs-integration-tests-best-practices icon indicating copy to clipboard operation
nodejs-integration-tests-best-practices copied to clipboard

How to test if a delete request actually deletes the resource

Open mikicho opened this issue 5 years ago • 4 comments

We have a user which has a few comments. When we delete the user, we should delete all its comments as well. How can we test that the comments get deleted? We can't do this through an API because the user doesn't exist anymore and:

GET /api/users/3/comments

Will return 404 error code because user 3 doesn't exist and not because user has 0 comments.

mikicho avatar Sep 25 '20 15:09 mikicho

Questions:

  • If the comments are not accessible now, it's an internal problem, is it important enough to test for this?
  • If the comments are accessible, for example using /api/artciel/1/comments, can't we just delete the user and try to access his comments through the article? #public-api-first

goldbergyoni avatar Sep 29 '20 06:09 goldbergyoni

  • Not from API POV, but for the developers to make sure the database is in the state they think it is. so yes IMO
  • if there is such an endpoint you can test it like this. but it's not always the case.

mikicho avatar Oct 02 '20 13:10 mikicho

Does the following rule make sense:

To minimize the tests coupling to the internals, approach it using the outer API that is available

  1. Great: HTTP API
  2. hmm OK: Public object (e.g. facade or service layer) [If it's not accessible by neither of those, I hardly see how those records have any meaning]
  3. Not good: Use the ORM/DAL public API
  4. Worst: Bypass the entire world and approach the DB directly

goldbergyoni avatar Oct 02 '20 14:10 goldbergyoni

LGTM

mikicho avatar Oct 03 '20 10:10 mikicho