As a learning excercise, I decided to take a hand on Test Driven Development.
Now I decided that there is a test I want to make; check if database connection doesn't leave any unsaved changes. The two options I can easily see are:
- I could put the assertion at end of every test method.
- I could add the assertion in cleanup which is ran after every test method.
I was initially considering to put the test at end of every test method, but that leaves a lot of code duplication, and could be missed out. On the other hand, putting assertion in cleanup ensures that the database never has unsaved changes, however it feels a like it violates Single-Responsibility of the cleanup function (which is to clean up resources).
[TestCleanup]
public void Cleanup()
{
Assert.IsFalse(dbContext.ChangeTracker.HasChanges(), "unsaved database changes detected.");
authContext.Dispose();
dbConn.Dispose();
}
This makes me wonder in testing is making asserts in cleanup a bad idea?