mardi 24 novembre 2015

Proper structure of a specific unit test for an HTTP handler that updates the DB

I have already managed to create a unit test that invokes the handler asynchronously, and asserts against a possible NULL value of the HTTP response, its headers and the return value. Nonetheless, this test is very naive, considering that this handler queries the DB, fetching some rows and later updating those rows.

What approach should I follow here? I have been thinking about using the following:

  • Run the programme setup, that will delete the (local) DB and populate it with configuration data.

  • Insert the necessary test data.

  • Invoke the handler (done already).

  • Assert about the return value of the asynchronous handler call (done already).

  • Query the DB, asserting whether its state corresponds to the expected state.

From the little I worked with unit testing, I know that the higher the isolation level in a unit test the better. Does the approach that I suggest guarantee the necessary isolation and, by testing against several distinct queries, with different outcomes, the desired test coverage?

PS: I am using C# and Xunit, but I think this question also applies to Java and other unit testing framewors

Aucun commentaire:

Enregistrer un commentaire