TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Testing Without Mocks: A Pattern Language (2018)

55 pointsby etamponiabout 3 years ago

7 comments

jdlshoreabout 3 years ago
Hey all, author here. I&#x27;m a bit late to the party but happy to answer any questions you have.<p>The main question seems to be: what&#x27;s the difference between null implementations and mocks?<p>There&#x27;s a practical answer to that question and a technical answer.<p>Practically speaking, the reason to use nullable infrastructure wrappers is that they enable sociable tests. A flaw of mock-based testing is that you only check that your system under test (SUT) <i>calls</i> its dependencies, but not that your code&#x27;s expectations of its dependencies are correct. Sociable tests check that the SUT uses its dependencies <i>correctly.</i> This is valuable when refactoring, because it&#x27;s easy to inadvertently introduce changes to behavior that break callers. Mock-based tests (&quot;solitary tests&quot;) can&#x27;t detect those errors.<p>Technically speaking, the nullable infrastructure wrappers convert your tests from interaction-based (&quot;did I call the right methods&quot;) to state-based (&quot;did my system return the correct results or make the appropriate state changes&quot;). They do this by adding state inspection capabilities to production code, allowing you to assert on the state of the production code rather than asserting which methods were called. They also implement a tiny <i>hidden</i> stub to enable you to &quot;turn off&quot; interactions with the outside world.<p>My approach and mocks&#x2F;spies are superficially similar, in that they both make use of test doubles (stubs, mocks, and spies are all test doubles) but they lead to significantly different testing approaches. Mocks&#x2F;spies lead to interaction-based solitary tests, and nullable infrastructure leads to state-based sociable tests. I also find the tests are easier to write and read, and they&#x27;re orders of magnitude faster¹ than using a mocking framework, too.<p>¹In a head-to-head comparison, nullable infrastructure ran 1,075,268 tests&#x2F;sec, testdouble.js ran 12,210 tests&#x2F;sec, and sinon.js ran 2,793 tests&#x2F;sec. <a href="https:&#x2F;&#x2F;github.com&#x2F;jamesshore&#x2F;livestream&#x2F;blob&#x2F;2020-05-26-end&#x2F;src&#x2F;_comparisons_test.js" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jamesshore&#x2F;livestream&#x2F;blob&#x2F;2020-05-26-end...</a>
评论 #30593329 未加载
loevborgabout 3 years ago
Enjoyed this much more than I thought I would<p>• Shows (almost) real-world examples to demonstrate the patterns<p>• Uses JavaScript, which is notoriously hard to test because of complex UI frameworks and difficult-to-mock APIs<p>• Set of patterns that refer to one another, providing mutual support<p>Would love to read more—any other resource along similar lines appreciated
评论 #30572134 未加载
评论 #30574121 未加载
评论 #30572216 未加载
karlmdavisabout 3 years ago
I’m really not clear what’s going on with the null implementations… aren’t those just very boring mocks?
评论 #30573841 未加载
评论 #30591618 未加载
评论 #30577429 未加载
评论 #30577248 未加载
tyingqabout 3 years ago
<i>&quot;Test-specific production code. Some code needed for the tests is written as tested production code, particularly for infrastructure classes. It requires extra time to write and adds noise to class APIs.&quot;</i><p>I don&#x27;t see this much in real world code. Is there a space&#x2F;niche that does this extensively? Does it raise a bunch of arguments about anti-patterns, etc?
评论 #30567862 未加载
评论 #30567790 未加载
评论 #30574182 未加载
评论 #30569652 未加载
ed-209about 3 years ago
Spoiler - expososing test data from the implementation: <a href="https:&#x2F;&#x2F;github.com&#x2F;jamesshore&#x2F;testing-without-mocks-example&#x2F;blob&#x2F;6e5e137e25933308b0146cc844aa01b561cd6986&#x2F;src&#x2F;command_line.js#L28" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jamesshore&#x2F;testing-without-mocks-example&#x2F;...</a>
评论 #30574137 未加载
bcbrownabout 3 years ago
This is almost exactly my approach to testing. I&#x27;m going to have to save this for sharing with teammates.
chezzwizzabout 3 years ago
I felt too compelled to stick with existing design pattern theories and maintain a sort of persistent stubbornness with traditional xUnit style tests and TDD rather than follow the rabbit on this article.