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.

Mocks Aren't Stubs (2007)

40 pointsby backslash_16over 8 years ago

6 comments

dparkover 8 years ago
I feel like this distinction is arbitrary and unintuitive. A mock is literally just something that mimics something else. All &quot;doubles&quot; can be considered mocks. Claiming that mocks <i>insist</i> on call verification seems like a very forced categorization. If there&#x27;s value in this distinction, maybe we need new terminology instead of asserting that everyone who&#x27;s conflated &quot;stubs&quot; and &quot;mocks&quot; for two decades is wrong.<p>As a side note, verifying call patterns makes for terrible, brittle tests that cost far too much to maintain. This pattern is great for enforcing call patterns that truly that matter for, e.g, performance reasons (no unexpected cascade of DB calls), but is typically just a giant pain that provides little to no value, because instead of testing that the code does the right thing, you&#x27;re testing that <i>it doesn&#x27;t change</i>.
评论 #12955114 未加载
评论 #12954877 未加载
评论 #12955164 未加载
cessorover 8 years ago
I often found that these terms are not very usefull when explaining the concepts of IoC in testing (which is essentially what they do) to colleagues who were unfamiliar with it. Programming in .NET I usually tried to use frameworks that avoided the terms, such as NSubstitute where you create an object by substitution:<p>var format = Substitute.For&lt;IFormatCode&gt;();<p>I haven&#x27;t used it in a while but I remember that Rhino Mocks asked for ridiculous code like<p>var format = MockRepository.GenerateStub&lt;IFormatCode&gt;();<p>(Which one is it, a Mock or a Stub? And: Does it matter?)<p>I recommend Gerard Meszaros excellent book XUnit Test Patterns, he makes more fine grained distinctions between different aspects of testing. I liked his notion that you simply create objects that produce or react to &quot;indirect input and output&quot; (i.e. exceptions, opcodes, sideeffects) rather than &quot;direct input and output&quot; (which is what &quot;arrange&quot; and &quot;assert&quot; are checking). Mocks and stubs are only realizations of different patterns.<p><a href="http:&#x2F;&#x2F;xunitpatterns.com&#x2F;" rel="nofollow">http:&#x2F;&#x2F;xunitpatterns.com&#x2F;</a>
评论 #12954746 未加载
taericover 8 years ago
Every validation you do on a mock is an assertion on outside behavior that is in no way coupled with your test. If that behavior changes, your test is now false. Not failing. False. That concerns me.
评论 #12954736 未加载
评论 #12954920 未加载
ternaryoperatorover 8 years ago
This article is almost universally cited when a book or article on testing or TDD begins the inevitable discussion of mocks&#x2F;fakes&#x2F;stubs. But in real life, this is a distinction without difference. I have never once heard someone use the term &#x27;mock&#x27; and be misunderstood because he&#x2F;she should&#x27;ve used &#x27;stub&#x27;.
backslash_16over 8 years ago
How do you test your code that interacts with external components, like sending emails to a mail server or making an external API call without using some sort of test fake or stub? (using the definition of them from the article)<p>I&#x27;m new to TDD and refactoring legacy code for testability. With the code architecture I have it looks and feels like I need either a mock, fake, or stub.<p>I&#x27;m using an IExternalComponent interface as a parameter and for the concrete implementation I can either pass the real thing (it&#x27;s non deterministic, this works if the service is up) or I can pass a test double that allows me to throw exceptions at will and force failure conditions to be tested.<p>Is there another way of doing this that I am missing?
born_of_dustover 8 years ago
Love this article.