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