I'm about to write a lot of test for my application. While some of the tests check whether everything works if the user enters the right data, most of them deal with wrong/missing data. In these cases nothing should be updated and an exception should be thrown.
At the moment, this leads to a lot of duplicated code, because every test has to make sure that not only the exception is thrown but that no data in my (
mocked in-memory) database is changed.
My first idea was to move these tests into a function and to call this function in every test. Pro: I still can use very explicit tests. Contra: I have to remember changing the function and adding more tests whenever I add more functionality to the checked function.
But I could as well add a property to my
mocked in-memory-repository that indicates whether an entity was updated. Pro: I'll never have to worry about a forgotten check. Contra: The check is quite vague - When it fails I'll only knew that something was changed. In order to find the error I'll have to debug step by step through the function.
What is the better approach? Maybe there's an even better third solution?