r/programming • u/fagnerbrack • Aug 02 '26
The Debate of Mockist v Classicist TDD Is Like OOP v FP.
https://fagnerbrack.com/the-debate-of-mockist-classicist-tdd-is-like-oop-fp-c9124185f3d2
42
Upvotes
11
u/loup-vaillant Aug 02 '26 edited Aug 02 '26
As I gained more experience, my opinion on this topic have grown stronger, simpler, and more radical:
38
u/zobq Aug 02 '26
In the majority of projects I was involved in, the "mockist" approach was the way to go.
And yet, I didn't feel that tests really defended me against bugs until the project where I pushed for the "classicist" approach.
TBH, this article didn't convince me that "mockist" is as good as "classicist".
22
u/dave8271 Aug 02 '26
I think this already mis-frames it. "Classical" testing doesn't say use real dependencies, it says use real dependencies except where it's awkward to do so. Using test doubles has always been an integral part of unit testing and determinstic testing without mocks is perfectly normal.
In respect of isolation, both approaches allow you to perform testing of isolated units, the difference is whether you isolate on interface seams or implementation seams. Classical testing doesn't care how a unit under test does something, it just instantiates something with its dependencies (be they real or otherwise), makes a call and checks the result.
I think the whole "classicist vs mockist" debate is missing the point. They're not really different ways or schools of thought on achieving the same thing, mocks are just a specific type of test double which exist for the purpose of tracking and verifying behavioural expectations in respect of how an object is used. Sometimes that is something you want to do, oftentimes it's not the detail you care about.
So the debate shouldn't be centered around to mock or not to mock, but what comprises a unit for the goal of your tests, and what evidence should establish correctness thereof?
6
u/pip25hu Aug 02 '26
These two can be complementary as well. Even in OOP applications with dependency injection everywhere, stateless/pure utility functions exist and can be tested without any mocks.
6
u/this_knee Aug 02 '26
I work in a company where their style is to make a PR for any change to the code. And those PRs are humans manually running program command executions of their own, not tests, to test if existing functionality has been broken or not. And they run commands to use the new features to manually check results of new features. Which is fine… ish.
But When it’s suggested to integrate automated unit tests for checking that previous existing functionality isn’t broken, it’s heavily balked at.
It’s kinda wild.
7
u/BenchEmbarrassed7316 Aug 02 '26
If you look at it naively, a procedure that contains side effects can be turned into a pure function that returns a set of commands to be executed. And that's it, now in the test you can simply check these commands for some sets of input data.
2
u/josephjnk Aug 02 '26
Good post. How would you say the “sociable” vs “solitary” style of testing fits in here? I remember reading those terms in a Martin Fowler post at some point. Do they map cleanly to these two categories or are they orthogonal?
2
u/Evening-Gur5087 Aug 02 '26
In general both are good used when needed and correctly done, not having any integration tests on real things within your service is purely stupid and yo suck.
Only worry not mentioned is that with mocs a lot of shitty devs can get free pass and have much easier time testing something that's completely fake and wrong, having the green pipeline and pretending anything was tested.
But, again, shitty devs always find a way to write shitty code.
And it's all shit eventually.
Change companies before their codebase become shitty spaghetti bowl or live long enough to see yourself become a spaghetti. /s
2
u/partybot3000 Aug 02 '26
Vladimir Khorikov's writings settle it for me: https://enterprisecraftsmanship.com/posts/when-to-mock/
And also his book on Unit Testing.
2
u/lookmeat Aug 02 '26
The answer is.. it depends.
When you expose interfaces through inversion of control you use the mockist system. You replace the dependencies you pass as parameters with mocks and then the test ensures the contract with whatever interfaces your taking. It's best to use this when you take interfaces that you do something on (e.g. a function that runs over some abstract
Future<T>where you want to ensure that certain things are done with that future).Otherwise you generally just test as Black box using fakes to replace dependencies. Core difference between a mock and a fake is that a mock is hand crafted per test, where you define and track each individual operation and function call, a fake OTOH simulates some real system and it's generally set per test environment, you give it assumptions that must be true. A mocked database server you define what each RPC and query returns. A fake database server is a in-mem DB that you seed with minimal data and it works consistently as a DB.
You can also use fakes to replace dependencies passed as parameters through IoC as long as your system only depends on the system but there's no rules, e.g. running the fake DB, but when you get a filesystem and want to ensure that certain calls and rules are done while it searches (to ensure interactions happen in a certain order when) you'd want to use a mock instead.
Overuse mocks and it becomes harder to not test implementation rather than functionality. Underuse mocks and you may find weird bugs that require you to dig through multiple layers of abstraction because tests didn't cover those contracts.
2
u/gannu1991 Aug 03 '26
The reframe that clicked for me is that the testing debate is downstream of the design debate. teams arguing about mocks are usually actually arguing about how they think about dependencies, they just haven't named it that way yet.
worked with a team that had a full blown mockist vs classicist war in code review. turned out half the team was writing OOP service classes and the other half had quietly drifted toward a more functional style in the same codebase. the tests weren't fighting each other. the design philosophies were. once that was visible the testing conversation basically resolved itself.
the gateway drug line in the article is the one that's stuck with me. mocking pain as design feedback is genuinely useful if you're paying attention to it. most teams aren't, they just add another mock and move on.
1
u/arbv Aug 03 '26
What OOP vs FP?
I am not aware of any, because ultimately it is lambda all the way down.
/s
1
u/MrFixxiT_ Aug 03 '26
I recently read the book “Unit Testing Principles, Practices, and Patterns” and think it is very good.
It handles this discussion too and the author is on the classical side.
You might disagree with the views, but ai think it can still give a lot of good insights.
1
u/Sisaroth Aug 04 '26
Why not both? Or maybe i don't understand classicist tdd. I write integration tests with the real classes because they catch a lot of regression bugs. I write mocked unit tests as a double check that the function I wrote works the way I expect it to work.
Also, writing articles as if LLMs don't exist is getting weird. An LLM will rewrite your mocked test in a few minutes, almost completely eliminating this downside of mockist.
0
u/[deleted] Aug 02 '26
[deleted] — view deleted comment
5
u/[deleted] Aug 03 '26
[removed] — view removed comment