Skip to content
BENIBIX Learn

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.

Development🧪 Testing & Quality

Mock (Testing)

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

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.
Why it matters
Some results never come back as a value, like an email sent. A mock lets you check them anyway.
Full entry
Development🧪 Testing & Quality

Stub (Testing)

A fake part that always gives the answer you chose, so tests stay the same.

A stub is a test double that returns a fixed answer you chose ahead of time. It does not check how it was called; it just answers. You use it to put your code in a known state, like a weather service that always says rain. That makes a test give the same result every time. Where a mock checks the call, a stub only feeds the answer.

Example
Lucy's app shows an umbrella when it rains. Her test uses a stub weather service that always returns rain, so the umbrella must show.
Why it matters
Real services change their answers. A stub lets you test each case on purpose, including the rare ones.
Full entry

In one line

Mock (Testing)
A fake part that records how your code called it, so a test can check.
Stub (Testing)
A fake part that always gives the answer you chose, so tests stay the same.