Skip to content
BENIBIX Learn

Mock (Testing)

Development🧪 Testing & QualityTest Doubles

Also known as Mock, Mock Object, Mocking

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

Think of it like

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

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.

Why it matters

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

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 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.

Common mistake

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

Key takeaways

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

Compare