DEV Community

Cover image for Code Smell 30 - Mocking Business
Maxi Contieri
Maxi Contieri

Posted on • Updated on • Originally published at maximilianocontieri.com

Code Smell 30 - Mocking Business

Mocking is a great aid when testing behavior. Like with many other tools, we are abusing them.

Problems

  • Complexity

  • False sense of security.

  • Parallel/Duplicated objects (Real and Mocks)

  • Maintainability

Solutions

  1. Mock just non-business entities.

  2. Remove mock if its interface has too much behavior.

Sample Code

Wrong

Right

Detection

This is an architectural pattern. It will not be easy to create an automatic detection rule.

Exceptions

  • Mocking accidental problems (serialization, databases, APIs) is a very good practice to avoid coupling.

Tags

  • Abuser

Conclusion

Mocks, like many other test doubles are excellent tools. Choosing wisely when to use them is an art.

Imagine a play in which each actor, instead of rehearsing with other actors, had to interact with 25 scriptwriters. The actors would never rehearse together. How would the result of the play be?

Also Known as

  • Faker

More info

Mocking is a code smell

Mocks aren't stubs

Credits

Photo by Syed Ahmad on Unsplash


The pesticide paradox. Every method you use to prevent or find bugs leaves a residue of subtler bugs against which those methods are ineffective.

Boris Beizer

Top comments (0)