惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

人人都是产品经理
人人都是产品经理
有赞技术团队
有赞技术团队
L
LangChain Blog
C
Check Point Blog
博客园 - 【当耐特】
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
GbyAI
GbyAI
美团技术团队
博客园 - 司徒正美
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
S
SegmentFault 最新的问题
A
About on SuperTechFans
Blog — PlanetScale
Blog — PlanetScale
Hugging Face - Blog
Hugging Face - Blog
博客园 - 叶小钗
腾讯CDC
B
Blog
G
Google Developers Blog
The Cloudflare Blog
P
Proofpoint News Feed

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Why I Prefer Chicago-Style TDD
Ian Johnson · 2026-05-13 · via DEV Community

There are two big schools of TDD, and most devs end up in one without ever really picking sides. Quick recap: London-style (mockist) drives design by mocking collaborators and asserting on interactions. Chicago-style (classicist) builds from the inside out using real objects and asserting on values and state.

I'm firmly in the Chicago camp. Here's why, and how it pairs really nicely with hexagonal architecture.

Mocks belong at boundaries. That's it.

I'm not anti-mock. Mocks are great when you're crossing a boundary you don't control or don't want to hit in a test: the database, an HTTP API, a message queue, the clock, the filesystem. Anything where the alternative is slow, flaky, or has side effects you can't take back.

Inside the boundary? Use real objects. If two domain classes collaborate, let them collaborate. Build them up, call the method, assert on what came out. That's the test.

The moment you start mocking your own internal types, you've stopped testing your system and started testing your assumptions about how your system should talk to itself. Those are very different things.

Mocking hides real problems

When you mock a collaborator, you're hand-writing what you expect it to return. That mock will happily return whatever you told it to, forever, even if the real thing's contract changed three refactors ago. Your test stays green. Production breaks.

Real objects don't let you get away with that. If the collaborator's behavior changes, the test that uses it will notice, because it's actually running through it. You get an early, honest signal. Mocks give you a comfortable, dishonest one.

Good tests don't care about implementation. Mocks force you to care.

This is the part that bugs me most. The whole point of a test, in my view, is "given this input, I expect this output (or this state change)." That's it. I shouldn't have to know, or care, how the code gets there. That freedom is what makes refactoring safe.

Mock-heavy tests destroy that. Now your test is asserting things like "the service called repository.findById exactly once with this argument, then called mapper.toDto, then…" You've baked the implementation into the test. The minute you reorganize the internals, even if behavior is identical, your tests light up red. That's not a useful signal. That's friction.

And here's the kicker: with strict mocking, you don't even need to implement the thing correctly. As long as the calls match the expectations, the test passes. You can satisfy a contract without honoring it. I find that genuinely unsettling. The test isn't proving the code works; it's proving the code makes the right phone calls.

Use real objects. Fake the rest.

My default is: real objects everywhere I can get away with it. When I can't (boundaries, again), I reach for fakes before mocks.

A fake is a real, working implementation that's just simpler. An in-memory repository that stores things in a map instead of Postgres. An email sender that appends to a list instead of calling SendGrid. The fake has actual behavior (you can put things in and get things out) so your test exercises a real interaction, not a scripted one.

For things I need to observe (was this notification sent? did we publish an event?), I use spies. A spy records what happened so I can assert on it after the fact, without dictating the shape of every internal call up front.

Then I test on values. Did the function return what it should? Is the system in the state I expect? Did the right event end up in the spy's recorded list? That's it. No "verify was called with." No call-order assertions. Just inputs and outputs and observable state.

Hexagonal architecture makes this almost free

If you've used hexagonal architecture (a.k.a. ports and adapters), most of the work for Chicago-style TDD is already done.

Quick refresher: your domain lives in the middle. It defines ports — interfaces that describe what it needs from the outside world (a UserRepository, a PaymentGateway, a Clock). Adapters are the concrete implementations that plug into those ports: a Postgres adapter, a Stripe adapter, a system clock.

The domain doesn't know or care which adapter it's running against. It just talks to the port.

In production, you wire up the real adapters. In tests, you wire up fakes: an in-memory UserRepository, a FakePaymentGateway that records charges, a FixedClock you control. Same port, different adapter. The domain has no idea anything changed, which is exactly what you want.

What you get:

  • Real domain logic actually runs in your tests. No mock-puppeteering. The classes you ship are the classes under test.
  • Fast tests. No DB, no network, no sleeps. In-memory fakes are essentially free.
  • Parallel ready. Since there is no contention for the database or network, the tests can be parallelized much easier.
  • Tests that survive refactoring. Move methods around, rename internals, split a class in two — as long as the port contracts hold and the outputs match, your tests stay green.
  • Mocks stay at the edges, where they belong. And often you don't even need them, because a well-written fake adapter does the job better.
  • The fakes become a design tool. If a fake adapter is painful to write, that's usually the port telling you it's badly shaped. Listen to it.

The architecture and the testing style reinforce each other. Hexagonal pushes side effects to the edges; Chicago-style TDD wants the middle to be real and the edges to be swappable. Same idea from two angles.

Wrapping up

Test the behavior of your system through its real objects. Push side effects to the boundary. Swap those boundaries for fakes when you test. Assert on values and state, not on call patterns.

You end up with tests that tell you when something is actually broken, stay quiet when you refactor, and let you change your mind about implementation without paying a tax. That's the whole job.

Mocks are a tool. A useful one, at the edges. But if they're showing up all through your test suite, your tests have stopped describing what your software does and started describing how you currently happen to have written it. Those two things should never be the same.