[EN] To mock, or not to mock, that is the question
Published: 2022-02-22 13:44:34

---
title: [EN] To mock, or not to mock, that is the question
published: true
date: 2019-12-01 00:00:00 UTC
tags: 
canonical_url: https://bpohoriletz.github.io/2019/12/01/to-mock-or-not-to-mock.html
---

- Time: 5-10 min
- Level: Beginner/Intermediate

The following checklist is aimed to help you protect yourself from improper usage of mocks and stubs.You may find yourself willing to add/remove items from the list – that is totally OK, just make sure that you stickto your final version.

Mocking has been a controversial practice from the very moment I’ve heard about it for the first time, however mostpeople agree that mocking is a tool and as any other tool it can’t be good or bad, rather it’s developers obligationto pick the right tool for the job and make most of the features it provides.

You’re doing it wrong if:

1. Mocking things you don’t own – External Service, IO, System, etc.    
  - if appropriate, add an Adapter as a wrapper around a thing and mock it instead
2. Mocking thing under test    
  - you may want to introduce and mock collaborator instead
3. Mocking trivial things    
  - you may use an actual thing instead of a mock
4. Mocking when you should be stubbing    
  - test for observable behavior instead of methods being called, methods are only an implementation detail
5. Mocking only a part of a collaborator’s interface    
  - mock it entirely or do not mock at all
6. Mocking at wrong level/layer    
  - for unit tests – mock your nearest neighbor
  - for regression safety – mock dependency at the lowest possible level

If you have not violated any of the upper items, and your tests still are brittle or make you cry at night – that may be an indication of a bad design. If that’s the case – it doesn’t matter either you mock or not because the problem exists between the chair and computer.

# References

- [Justin Searls – Please don’t mock me](https://www.youtube.com/watch?v=Af4M8GMoxi4)
- [Avdi Grimm - Episode #287: Mocking Smells 1](https://www.rubytapas.com/2015/03/05/episode-287-mocking-smells/)
- [Avdi Grimm - Episode #289: Mocking Smells 2](https://www.rubytapas.com/2015/03/12/episode-289-mocking-smells-2/)
- [Avdi Grimm - Episode #296: Mocking Smells 3](https://www.rubytapas.com/2015/04/06/episode-296-mocking-smells-3/)
- [Avdi Grimm - Episode #312: Mocking Smells 4](https://www.rubytapas.com/2015/06/01/episode-312-mocking-smells-4/)
- [Avdi Grimm - Episode #052: The End of Mocking](https://www.rubytapas.com/2013/01/28/episode-052-the-end-of-mocking/)
- [Robert C. Martin - The Little Mocker](https://blog.cleancoder.com/uncle-bob/2014/05/14/TheLittleMocker.html)
- [Robert C. Martin - When To Mock](https://blog.cleancoder.com/uncle-bob/2014/05/10/WhenToMock.html)
- [Kent Beck - Programmer Test Principles](https://medium.com/@kentbeck_7670/programmer-test-principles-d01c064d7934)
- [Martin Fowler - UnitTest](https://martinfowler.com/bliki/UnitTest.html)
- [Martin Fowler - The Practical Test Pyramid](https://martinfowler.com/articles/practical-test-pyramid.html#MockingAndStubbing)
- [Bill Wake - 3A – Arrange, Act, Assert](https://xp123.com/articles/3a-arrange-act-assert/)
- [James Shore - Testing Without Mocks: A Pattern Language](https://www.jamesshore.com/Blog/Testing-Without-Mocks.html)