Mocks vs Stubs

Style

Mocks vs Stubs = Behavioral testing vs State testing

Principle

According to the principle of Test only one thing per test, there may be several stubs in one test, but generally there is only one mock.

Lifecycle

Test lifecycle with stubs:

  1. Setup – Prepare object that is being tested and its stubs collaborators.
  2. Exercise – Test the functionality.
  3. Verify state – Use asserts to check object’s state.
  4. Teardown – Clean up resources.

Test lifecycle with mocks:

  1. Setup data – Prepare object that is being tested.
  2. Setup expectations – Prepare expectations in mock that is being used by primary object.
  3. Exercise – Test the functionality.
  4. Verify expectations – Verify that correct methods has been invoked in mock.
  5. Verify state – Use asserts to check object’s state.
  6. Teardown – Clean up resources.