Is using Auto Mocking containers a good or bad practice?

I recently worked on a project that began to heavily influence addiction and explored the idea of ​​using an AutoMocking container to clean up my tests a bit and make them less fragile.

I heard arguments against TDD / BDD purists using them, stating things like: It’s not immediately obvious which dependencies are required by the test subject, or you can add dependencies that you really don’t need. None of them seem to be a particularly strong argument against their use.

From my point of view, introducing one of them, I would allow refactoring as necessary, removing and injecting dependencies in accordance with business requirements, without constantly returning to tests and introducing new mocks / stubs to get code for compilation.

Is AutoMocking Good / Bad Practice? Are there specific situations where this should or should not be used?

+6
source share
3 answers

As with any tool or process, there is the right time and the wrong time to use them. Nothing is a silver bullet. You must ask yourself: "Will this help me do my job?" And at the end of our day, our task is to provide the greatest business value for the dollar.
When you are developing new fields, automation is not so useful. Everything develops from scratch, and TDD / BDD methods with “traditional” mocking work are great. Theoretically, dependencies do not change so dramatically, and when they are, it is probably good to know when you are breaking things.

When in maintenance mode (or with an outdated code base), autonomy can be extremely useful. For example, you are tasked with clearing technical debt. This will probably be due to a lot of refactoring, and the ability to isolate your tests from these changes is a huge time margin. Keep in mind that if your code has many dependencies, it probably violates SOLID and SOC, and you (or at least should) have many tests that do not use all the dependencies. Thus, automation in this case is extremely useful. Of course, there are many other examples where this helps.

Like any tool, you must make sure that it does not become a crutch. Using automocking so you can change interfaces, and apis willy-nilly is obviously an anti-pattern. But when used correctly, I found this a huge advantage for our project teams.

It just requires critical thought and application in the right scenarios.

+5
source

Manually connect your dependencies (and keep in mind that in unit tests your dependencies should be for a very small number of subject objects (one)) lets you know when you have a smell. This class is too damned and needs to be cut down. However, I do not think that automatic bullying is bad, but, like every tool, should be used with caution.

+2
source

Auto-mockery only starts to be useful when addictions start to smell. Both Skimedic's answer and levelnis comment point in the same direction. Therefore, this practice should be avoided altogether, unless you cannot get rid of a debt obligation. Even in some cases, I think that it’s better to act like automatic mockery does not exist, and let the reduced speed of the team become another reason to make the management believe that some kind of refactoring is in order; or, if this is some inevitable legacy, then program your own fake builders; the added complexity of the test will be a better marker of the “danger zone” than just using automatic bullying and apparently simple tests.

And, IMO, you should not use it at all with the new code.

0
source

All Articles