Overview

Mocks are used to isolate the code being tested from its dependencies (like databases, APIs, or other classes). They allow you to verify that the code interacts with its dependencies in the expected way.

Key Features

  • Expectations: You can tell a mock what methods should be called and with what arguments.
  • Verification: After the test, you can check if those expectations were met.

Mocking vs. Stubbing

While both provide canned responses, mocks are primarily used to verify interactions (behavior), while stubs are used to provide data (state).

Related Terms