DEV Community

Discussion on: When should I (not) use mocks in testing?

Collapse
 
myshov profile image
Alexander Myshov

Hey Artem, thank you for the article

Can you please explain this section more?

The issue here is that if context.getUser stopped relying on the lookupSessions method the test would fail. Even if context.getUser still returns the proper user.

And I found a typo in the code example: lastSession.user should be latestSession.user

Collapse
 
kettanaito profile image
Artem Zakharchenko

Hi, Alexander! Thank you for spotting the typo!

That section referred to the implementation detail testing: assertion of how the software works instead of what it does. The getUser method can be implemented differently. For example, in the future, you may decide that you can resolve the user from elsewhere than sessions (abstract examples). If your test checks that lookupSessions must be called, the test would fail, while the functionality remains.

The goal of any test is to verify the intention behind the implementation, but not the implementation itself. Relying on the latter is often what makes tests brittle, and, most importantly, useless when refactoring.