Skip to main content

Mocking

Mocking replaces a real dependency — a service, a database, an external API — with a fake, controllable stand-in during a test, so the test can run in isolation, quickly and predictably, without needing the real dependency available or behaving unpredictably.

A mock doesn't just return canned data — it typically also lets a test verify how it was called: was this function invoked exactly once, with these specific arguments. This makes mocks especially useful for unit testing code that has side effects (like sending an email or writing to a database) without actually triggering that side effect in every test run.

Mocking trades realism for speed and control — over-mocking can leave a test suite passing while the real integration between components is actually broken, which is why mocks are typically paired with a smaller number of real integration tests that exercise the actual dependency, not a fake of it.

Mocking — Definition, Example & How It's Used | QA Bash Glossary | QA Bash