Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 22, 2022 10:57 am GMT

Why Mocks Are Considered Harmful

Automated testing during software development involves many different techniques, one that shouldnt be used is mocking. Mocks are a distraction at best and provide false confidence atworst.

Mocks Considered Harmful

What isMocking?

It is common for software developers to use mocks to simulate behaviour of code for network calls to other services or for database access. This enables unit tests to be run that areboth:

  • Fast because they dont need to rely on additional services.
  • Stable because they avoid availability issues.

This means that mocks are generally used for code with side effects, which is code that relies on or modifies something outside its parameters. This lets us classify functions as:

  • Pure: A function without any sideeffects.
  • Impure: A function that contains one or more sideeffects.

Pure vs Impure Functions

The Problems withMocks

**Mocks arent equivalent to the integrations they replace.

**If you mock a database client then you havent tested the integration with the real client. This means that your code may work with the mock but you will still need to do integration testing to make sure it works withoutmocks.

**Feature Parity Is Not Feasible.

**If you make a quick mock then it wont return useful data. The more time you spend improving the mock the more useful the data will be. However it can never be a true representation.

**Mocks that arent used are a waste of time and effort.

**If you mock out a database client and dont use it then there is no point mocking it. This can occur if some code requires valid configuration to initialise but doesnt useit.

Mocks Are Not Equivalent

How Do We ReplaceMocks?

Mocks are used to provide speed and stability but we can manage this in otherways.

Refactor your code! We can replace the need for mocks by separating the pure from the impure functions. Pure functions can be unit tested without mocks and impure functions should only be integration tested.

Code Refactoring Example

Improve Your Automation! By automating software packaging, deployment, and testing we can focus on integration testing faster instead of relying on unit tests. This also enables continuous delivery and reduces the impact of it works on my machine which are beneficial in modern software development.

Automated Build, Deploy, &Test

Summary

Mocking is a short term solution and a long term problem. If you want to deliver software faster then you should spend less time on mocks and more time on refactoring and automation.

If you would like to see more content like this follow me onmedium.

Let me know your thoughts on Twitter @BenTorvo or by Email [email protected]


Original Link: https://dev.to/bentorvo/why-mocks-are-considered-harmful-231h

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To