WIP – Mocks: is more always better?
Mocking is an important testing technique to master. Modern frameworks are fun to use and isolate tested code from dependencies like a breeze. For many, testing becomes synonymous with mocking.
But is there a downside to this power?
Yes.
Mocks tend to make our tests more verbose, dependent on implementation details and sometimes even telling an outright lie. Mocks add accidental, unnecessary friction to the code changes. The power of mocking libraries makes “testability as a way to improve the code” a stylistic exercise.
I argue, the mock-infestation of tests is one of the major causes many are sceptical about the power of developer tests.
What if we could magically remove the mocks, and make tests like pre-2000’s? I did this experiment once, and will share the outcomes in this presentation.
Not to suggest the mocks should be banned, I’ll offer a decision framework for when they are OK, and when it’s better to avoid using mocks in tests, and what are alternatives.
The examples will be provided in Kotlin (backend) and typescript (react), and are intended to be readable by any technical audience.
Inspiration
Devoxx Poland 2019: Automated tests: You won’t find it in a book! - Jacek Milewski
https://martinelli.ch/why-i-dont-use-mocking-frameworks-and-why-you-might-not-need-them-either/