Why Mocks Can Make Testing Hard
Mocks often force your tests to know exactly how your code works inside. This means if you change the internal steps of your code, even if the final result is the same, your tests might break.
Test What Your Code Does, Not How
The best way to test lets you change your code's inner workings without breaking the tests. Instead of checking if certain functions were called on a mock object, focus on the outcome or behavior of your code.
Easy Refactoring
When your tests focus on results, you can improve or rewrite parts of your code internally (refactor) and your tests will still pass as long as the code still does the right thing from the outside. This makes maintaining and improving your code much simpler.
An Example Shown
The example in the image shows testing if a notification was successfully received and its content, rather than checking if a specific 'send' method was called on a mocked object.
Move Beyond Restrictive Mocking
Frameworks or approaches that require you to mock and verify every single interaction can make your tests fragile and difficult to update. Focusing on behavior frees you from this constraint.