Skip to content

Resource · Guide or article

Mocking in Unit Tests

Compares three kinds of test double, stubs, mocks and fakes, using Martin Fowler's definitions, and argues for fakes where mocks would tie tests to implementation. Suited to engineers who write unit tests and expect follow-up questions on how they isolate dependencies.

Reviewed

Open the original: Mocking in Unit Tests (opens the original site in a new tab)

Add to a roadmap

Keep this resource with a roadmap you follow. It appears as your own extra resource; the roadmap's steps don't change and it doesn't count toward your tracked steps.

Source content: English

One of the key components of writing unit tests is to remove the dependencies your system has and replacing it with an implementation you control. The most common method people use as the replacement for the dependency is a mock, and mocking frameworks exist to help make this process easier.

PrepKit overview

PrepKit editors wrote and reviewed this overview from the source repository. It is not text from the resource itself.

Suited to
The source does not state a level
Format
Guide

What it covers

  • Stubs: canned responses, no framework required, but heavy boilerplate across tests
  • Mocks and behavioral verification versus state verification, with an example where a test asserting five web calls breaks after a refactor to one call
  • Fakes such as an in-memory database: working shortcuts that can carry their own tests and move into product code
  • Dependency injection as the precondition for swapping in a double
  • C# examples showing why asserting a mock's own return value proves little, and how a callback captures only the parameters you care about

How to use it

PrepKit study advice, not a claim made by the source.

Find a unit test you wrote that uses a mock and ask whether it checks behavior or state. Rewrite one assertion so it targets your code's logic instead of the mock setup. You are prepared when you can explain the stub, mock and fake difference with an example from your own code.

Scope and limitations

Team engineering guidance, not interview material. Code samples are mostly C# with the Moq style Setup and Verify syntax, plus one short Python stub, so readers using other stacks must translate. The anti-mock stance is the page's opinion.

Access and format

Free English web page in Microsoft's Code-With Engineering Playbook on microsoft.github.io (CC BY 4.0 repository).

Reviewed by PrepKit on Oct 2, 2026. Listing source commit: 016770e43d8a

Evidence behind this overview
  • docs/automated-testing/unit-testing/mocking.md

Pages that include this

Get the PrepKit newsletter

Optional. You don't need it to use PrepKit.

How we handle your email