I frequently find myself in the situation where I'm writing a unit test for a function that does for instance the following:
- Get object A from a store
- Get object B from a store
- Perform some operation on objects
- Do something else
Every time this happens I happly start with the unit test that checks if the store is accessed correctly for object A and mock the result.
But then I want to check to see if object B is retrieved correctly... for this I also need to mock the object A stuff, or the method will never come to the object B step.
For the operation part (usually the most interesting thing of the method) I have to mock 2 things before I can get started.
This is all fun, but you can imagine that the "arrange" phase of the unit test that checks the "Do something else" step will become pretty elaborate. Especially if the A and B objects require some mock setup to function (e.a. return some valid data for the method to work with)
How should I approach unit testing in these scenario's? For some reason most Unit testing blogs or Pluralsight courses manage to avoid this problem by using crazy simple examples that are not quite production code.
Aucun commentaire:
Enregistrer un commentaire