Skip to content

Stop Mocking Your Database in Tests

Alex Chen·April 7, 2026·1 min readDevelopment

The Problem

Every time you mock your database layer in tests, you are testing your mock, not your code. The mock says “when called with X, return Y” — but what if the real database behaves differently? What if the query has a subtle bug that the mock hides?

A Better Approach

Use a real database in your test suite. Docker makes this trivial — spin up a Postgres or MySQL container, run migrations, seed test data, run tests, tear down. Your tests hit real SQL, real indexes, real constraints. When they pass, you know the code actually works.

Yes, it is slower than mocked tests. But slow tests that catch real bugs beat fast tests that give false confidence.

Share:XFacebookLinkedInEmail
Alex Chen
Alex Chen

Senior Developer & Technical Writer

Full-stack developer and writer. I build things with Next.js, WordPress, and too much coffee. Currently exploring headless CMS architectures and AI-assisted development.

Leave a comment

Add a comment

Keep Reading

No image
Alex Chen
Alex Chen
Apr 7, 20262 minDevelopment

Building a Component Registry Pattern in Next.js

The Problem When building a themeable application, you need a way to render different components based on configuration. A page layout might specify “render a Hero section here, then a Latest Posts grid, then a Newsletter signup” — but the actual component implementations should be swappable. This is where the component registry pattern comes in. […]