Why a mock doesn’t work
Agreed with the articles, mocks are very tied to implementation details. I almost always prefer state-based testing:<p><a href="https:&#x2F;&#x2F;martinfowler.com&#x2F;articles&#x2F;mocksArentStubs.html" rel="nofollow">https:&#x2F;&#x2F;martinfowler.com&#x2F;articles&#x2F;mocksArentStubs.html</a><p>As &quot;observable state goes from a to b&quot; is much closer to a business&#x2F;functional requirement that will still&#x2F;always be true, regardless of refactorings.<p>Refactoring in codebases with state-based tests is a pleasure; in codebases with mock-based tests it&#x27;s tedious, constantly updating tests when no semantic behavior was supposed to change.<p>Also, mocking via module hacks like in the article (and in the JS world) is scary; modules are basically global variables so it&#x27;s a very coarse grained slice point. Dependency injection is almost always better.