Junior — Middle
What is the difference between mock objects and stubs in testing?
sobes.tech AI
Answer from AI
In testing, mocks and stubs serve as replacements for real components but have different purposes:
-
Stub — a simple object that returns predefined data so that the test can operate with predictable behavior. It does not check how it is called, just provides the necessary responses.
-
Mock — an object that not only mimics behavior but also verifies that methods were called with expected parameters and in the correct order. Mocks are used to verify interactions.
Example:
- A stub might return a fake response from a network request.
- A mock will check that the request method was called exactly once with a specific URL.
In iOS, frameworks like OCMock or Cuckoo are often used to create mocks and stubs.