DEV Community

Discussion on: Why Mocks Are Considered Harmful

Collapse
 
jessekphillips profile image
Jesse Phillips

I need to start by stating that there is a difference between unittest mocking and integration test mocking.

Bringing logic into pure functions for unit testing is definitely recommended over mocking. Try to reduce the dependency graph for any task is great in this regard. Unit testing can't connect to external systems or services as that is integration.

Then you have integration testing where different systems can be tested for integration. You cannot avoid integrating with real system.

If you build out a good mock system it not only provides faster more reliable test automation, it makes it possible to run tests not possible when using a live system (namely a not live system).

Is maintaining mocks more work? Yes. Is it always worth it? I think it usually is.

Here is what speed gets you. You refactor code and can be confident it operates as expected.

I have to be honest that I have no interest in mocking a database, this is likely a combination of the complexity for what a database does and its stability as a reliable service.