Today i want to talk about a feature of Fake It Easy that you can need in many cases:
If you have a method that retrieves you job ids that can be processed by the calling method (e.g. a print service with a job id provider that observes the database for new jobs). In my case, if there is no job, the method returns –1. But there are some circumstances in which my providers also returns –1 for example when i stopped my provider (in case of multithreading) or when an expected exception occurs (when the database is down).
So i don’t want to talk about how clean this code is if it acts like the way i told you, instead i want to make clear that this are two kinds of tests you’ve got to write:
- Case 1: There is no next job in the database
- Case 2: Any other cases with the same return value but another meaning/behaviour inside your method
The extension methods ‘MustHaveHappened’ and ‘MustNotHaveHappened’ are your friends:
If you’re wondering about the syntax (It should_…), so take a look here: MSpec
To quote ‘Barney’ from ‘How i met your mother’: “It’s awesome!”
The code above shows the solution for case 2: Though the second assert ensures that the return value is the default value, the first assert is the important one as it guarantees me that there was no read operation on the database
If i want to indicate case 1, i just have to rewrite line 5 into “.MustHaveHappened()”.