How to write a good unit test without mocking everything?

I read that they mock everything badly.
Test smell: all mocks
Layout everything is a good way to sink

I also read that unit tests examine a single component, while integration tests test an entire system working together.
Writing Great Tests: Best and Worst Practices

It bothers me. As far as I understand, to write the correct unit test, you need to isolate one component, taunting everything except SUT. If real objects are used throughout the test, is this integration test tested?

How to write a good (isolated) unit test without mixing everything?

+5
source share
1 answer

The mocking model is a bad smell. You should only make fun of the dependencies that are involved in the logic of the test method, not the data. But even with this simple rule, things are not always obvious.

In some cases, unit testing of a method is straightforward because the method has its own logical rules and also has one or two calls for a separate external dependency that is executed to complete its logic. In this case, mockingly seems natural.

In some other cases, unit testing is less obvious because the testing method has very few logical rules. In his implementation, he mainly addresses external dependencies. In this case, the mockery seems really unnatural, since the essence of the test can be a mockery. Testing a stream call with a mockery of each call checks the white box. It is fragile because it does not test the behavior of the method logic. This proves only one thing: your code does what it does.

In this case, I believe that integration tests should be approved for unit tests.

+4
source

Source: https://habr.com/ru/post/1266151/


All Articles