# Mock (Testing)

> A fake part that records how your code called it, so a test can check.

**Field:** Software Development  
**Category:** Testing & Quality  
**Also known as:** Mock, Mock Object, Mocking

## Think of it like

Think of it like a toy phone. It writes down each number you dial.

## Definition

A mock is a test double that records how it was called, so the test can check it. You hand it to your code in place of the real part. After the code runs, you ask the mock: were you called, how many times, and with what? That lets you test that your code sent an email without sending one. Most test tools have mocks built in.

## Example

Emma's test gives her sign-up code a mock email sender. The test then checks the mock was called once, with emma@example.com.

## Example code

```python
send = Mock()
send("emma@example.com")
send.assert_called_once_with("emma@example.com")
```

The mock remembers the call, and the last line checks it.

## Why it matters

Some results never come back as a value, like an email sent. A mock lets you check them anyway.

## Common mistake

Checking every call a mock got, down to the order. Small, harmless changes then break the test.

## Three principles

- **Records calls.** Who, how often, with what.
- **Checked after.** The test asks the mock.
- **Tests side effects.** Without the real effect.

## Related terms

- [Test Double](https://learn.benibix.com/term/development/test-double)
- [Stub (Testing)](https://learn.benibix.com/term/development/stub)
- [Spy (Testing)](https://learn.benibix.com/term/development/spy)
- [Side Effect](https://learn.benibix.com/term/development/side-effect)
- [Mock Server](https://learn.benibix.com/term/development/mock-server)

## Told apart from

- [Mock vs Stub](https://learn.benibix.com/compare/mock-vs-stub): A mock records how your code called it, and the test checks those calls afterwards. A stub only hands back an answer you chose ahead of time and checks nothing. Use a stub to set up a case, and a mock to prove a call happened.
- [Mock vs Mock Server](https://learn.benibix.com/compare/mock-vs-mock-server): A mock is a fake object inside one test, in the same program. A mock server is a fake API that runs on its own, over the network. Many tests and apps can call the same mock server.
- [Spy vs Mock](https://learn.benibix.com/compare/spy-vs-mock): People think a spy is just another name for a mock, and it is not. A mock is told up front which calls to expect, and it fails the test if they do not come. A spy only writes down every call. The test then looks at that record after the code has run and decides what to check.

---

Source: https://learn.benibix.com/term/development/mock
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-17
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
