'What's the point of writing unit tests with mocks, if it cannot catch changes in the source code?

I started writing unit tests recently. When I try to test the service of the Angular component, I usually mock it. Therefore, if someone deletes the line of the code, in the source code, that uses the service's method, the unit test still passes. What's the point of writing unit tests with mocks, if it cannot catch this type of code/logic disappearances?



Solution 1:[1]

Ideally, you have not to mock service under test. You must (or may) mock dependences of this service or, sometimes, some of the method of this service.

And when you test service - you test all public method of thos service. If someone delete public method from service, service's test fail.

But if you test another class which uses service - you test only all public method of class is that case. That class's test may not to fail.

Solution 2:[2]

If you are writing tests for a component and want to make sure a mocked service is called, you can do so in this way:

expect(myMockedService.theMethodIExpectToBeCalled).toHaveBeenCalled()

If the line of code that does the expected call is removed, this expectation will fail.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 alezhu
Solution 2 slim