How to write unit tests... In practice
Namely: 0. Separate side effects, write pure functions
- Create mocks for the happy path, with arbitrary values
- Consider every unhappy path and write a test for that, passing mocks based on the ones created above, with only the error condition changed (with values of the shape
some value
instead ofarbitrary value
, so I know those are relevant to the test). - Finally, tests for the happy path(s), with explicit successful mocks.
- (Potentially write the code, when doing TDD.) Then, check code coverage - any paths I missed that need tests? If irrelevant, mark as skipped for coverage with a comment explaining why.
Edited by Vincent