Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replaced mock with fake/stub for SendMoneyServiceTest #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

gerlacdt
Copy link

@gerlacdt gerlacdt commented Jul 8, 2022

Hi,

I read your book Get Your Hands Dirty on Clean Architecture and I liked it very much. I almost agree with everything on it.

But one thing is bothering me: it's the usage of mocks in tests. From experience, I know, tests with mocks are brittle. Further the test code is deluged with mock-statements and harder to comprehend.

The main purpose of hexagonal or clean architecture is loose coupling. I wonder why we toss the good intention overboard for our test code?!? With mocks, we expose the internals of our implementation classes and therefore we loose the ability to refactor the inner details of our service classes. Whereby refactoring is a major reason, why we write tests after all.

This PR offers a small taste, how tests could look like with fakes/stubs instead of mocks. Regarding the tests, I tried to assert one behaviour per test in order to assure a test breaks because of a single reason, this narrows down the potential problem area.

Mocks could be useful in rare cases though. The classic example is: testing a cache. With state verification, you can only check the returned value, but one would not know if the value came from the database or from the cache. With mocks, one can verify that the first call returned from the database and the second call returned from the cache.

I hope the PR is a bit helpful :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant