Your tests are mocking you - Jakub Zalas

Learn how to write effective unit tests using test doubles, stubs, and spies, and avoid common pitfalls such as over-specifying implementation details and testing entire apps at once.

Key takeaways
  • Mocking frameworks often misused and hated, but also useful when used correctly.
  • Problem with mocking is that it makes tests brittle and difficult to understand.
  • Instead of mocking, use test doubles (stubs and spies) to isolate dependencies.
  • Use entity manager to persist data, not in-memory repository.
  • Test a single function or class in isolation, rather than trying to test entire app.
  • Use revealing names for variables and methods to make tests more readable.
  • Test for indirect output, not just direct output.
  • Use edge cases to test behavior.
  • Avoid over-specifying implementation details in tests.
  • Test for exceptions and side effects.
  • Use value objects to make tests more readable.
  • Create a contract test for a function or class, then create implementation.
  • Use PHP’s mocking framework to create stubs and spies.
  • Test for expected calls to collaborators.
  • Use expectation objects to specify expected behavior.
  • Verify if arguments are what you expect them to be.
  • Track if dependencies are called or not.
  • Verify if dependencies return expected values.
  • Test for side effects, such as changes to state.
  • Use in-memory repository to test data persistence.
  • Test for expected exceptions and errors.
  • Use revealing names for test cases to make them more readable.
  • Use built-in mocking framework to create stubs and spies.
  • Test for behavior, not just implementation details.
  • Create a contract test for a function or class, then create implementation.
  • Use test doubles (stubs and spies) to isolate dependencies.
  • Test for indirect output, not just direct output.