Get The Book
Tales Of TDD: One Test Double To Rule Them All
March 5, 2026So I’ve encountered this specific test case, which is quite elaborate.
What do you mean by that?
Well, it contains a lot of code, and also looks more difficult to understand compared to the other test cases.
Yeah, that’s because of the Fake we’re using in our test code.
So, the test code is complex because we’re using a Fake?
Yes, I’m afraid so. The test scenario embodies a specific edge case. The Subject Under Test should initiate a recovery
only when the collaborator raises an InvalidValueException.
That does align with the functionality we discussed earlier.
I had to do all this setup in the test method just to force the Fake to throw an InvalidValueException.
I see. I agree that it’s sometimes not easy to make a Fake behave exactly as you want.
Tell me about it. It took me almost an hour to figure out how to make it throw an exception. I really like using Fakes in our tests because they behave just as the real implementation. But sometimes, they can be a real pain to work with as well.
Have you considered using a Stub instead of a Fake?
A Stub? No, not really. We decided in a team meeting to use Fakes instead of Stubs, Spies, or Mocks
I understand. I’m not suggesting you stop using Fakes altogether. But have you considered not using a Fake just for this
specific test case? Would it make the test easier to understand if you used a Stub that raises an InvalidValueException
instead?
Yeah, but that’s not what we agreed on in the team meeting.
I’m sure what was agreed on was more of a guideline than a strict rule. How about we just try it out? Let’s change the test code to use a Stub instead of a Fake.
OK. We can do that.
… After short while …
Now the test is just four lines of code. What do you think?
Yeah, it’s a lot more concise and readable.
Do you want to keep it like this?
Sure! But shouldn’t we change all the other tests to use a Stub for that collaborator as well?
Why do you want to do that?
Because that way, it’s more consistent.
If consistency is what we’re after, then I agree with you. But what do we ultimately want from our tests?
That they fail when we have an issue in the production code.
Yes, and what else?
That they are readable and easy to understand.
Exactly! There’s no reason to be dogmatic about test doubles. We can just use the one that best fits our purpose. So it’s perfectly fine to use Fakes for most of the tests, while using a Stub for this specific test case as a one-off.
I see that now. So I don’t need to stick with one specific type of test double? That’s very cool. I can’t wait to show this off at our next team meeting.
If you and your team want to learn more about how to write maintainable unit tests and get the most out of TDD practices, make sure to have look at my trainings and workshops or check out the books section. Feel free to reach out at info@principal-it.be.
Jan Van Ryswyck
Thank you for visiting my blog. I’m a professional software developer since Y2K. A blogger since Y2K+5. Provider of training and coaching in XP practices. Curator of the Awesome Talks list. Past organizer of the European Virtual ALT.NET meetings. Thinking and learning about all kinds of technologies since forever.
Comments
Get The Book
Writing Maintainable
Unit Tests
Watch The Videos
Latest articles
-
Tales Of TDD: One Test Double To Rule Them All
March 5, 2026
-
Perspectives On Software Quality
February 3, 2026
-
The Five Underplayed Premises Of TDD
August 8, 2025
-
Contract Tests - Parameterised Test Cases
June 28, 2023
-
Contract Tests - Abstract Test Cases
April 12, 2023
Tags
- .NET
- ALT.NET
- ASP.NET
- Agile
- Announcement
- Architecture
- Behavior-Driven Development
- C++
- CQRS
- Clojure
- CoffeeScript
- Community
- Concurrent Programming
- Conferences
- Continuous Integration
- Core Skills
- CouchDB
- Database
- Design Patterns
- Domain-Driven Design
- Event Sourcing
- F#
- Fluent Interfaces
- Functional Programming
- Hacking
- Humor
- Java
- JavaScript
- Linux
- Microsoft
- NHibernate
- NoSQL
- Node.js
- Object-Relational Mapping
- Open Source
- Reading
- Ruby
- Software Design
- Software Quality
- SourceControl
- Test-Driven Development
- Testing
- Tools
- Visual Studio
- Web
- Windows
Disclaimer
The opinions expressed on this blog are my own personal opinions. These do NOT represent anyone else’s view on the world in any way whatsoever.
About
Thank you for visiting my website. I’m a professional software developer since Y2K. A blogger since Y2K+5. Author of Writing Maintainable Unit Tests. Provider of training and coaching in XP practices. Curator of the Awesome Talks list. Thinking and learning about all kinds of technologies since forever.
Latest articles
Tales Of TDD: One Test Double To Rule Them All
Perspectives On Software Quality
The Five Underplayed Premises Of TDD
Contract Tests - Parameterised Test Cases
Contact information
(+32) 496 38 00 82
info@principal-it.be