lundi 1 juin 2020

How should I assert in WebAPI integration test?

I'm writing integration tests for Controller classes in my .NET Core WebAPI, and honestly, I'm not sure if Assert.Equal(HttpStatusCode.OK, response.StatusCode); is enough. I'm using my production Startup class with only database different (using InMemory database).

This is one of my tests, named ShouldAddUpdateAndDeleteUser. It's basically:

  1. Send POST request with specific input.
  2. Assert If POST worked. Because POST sends respond with created object, I can Assert on every property and if the Id is greater then 0.
  3. Change the output a little bit and send Update request
  4. Send GET request and assert if update worked.
  5. Send DELETE request
  6. Send GET request and assert if null.

Basically, I test, ADD,UPDATE,DELETE,GET (when item exists), GET (when item doesn't exists).

I have a few questions:

  1. Is it a good practice to have such tests? It does do a lot, but it's not a unit test after all. If it fails, I can be pretty specific and specify which part didn't work. Worst case scenario I can debug it pretty quickly.
  2. Is it integration tests or functional test or neither?
  3. If this is wrong, how can I test DELETE or UPDATE? I'm kinda forced to call GET request after them (They return NoContent)

(Side note: It's not the only test I have for that controller obviously. I also have tests for GET all as well as BedRequest requests)

Aucun commentaire:

Enregistrer un commentaire