MEPX
Chapter 9 of 10All chapters

Chapter 9 of 10

Testing object code

What makes a class easy to test.

Seams

A class that creates its own database connection cannot be tested without one. The same class receiving that connection can be handed a fake, which is why injection matters.

  • Test through the public interface, not the private internals.
  • If a test needs to reach inside, the design is telling you something.

Fakes and mocks

A fake is a simple working substitute; a mock records how it was called. Mocking everything ties tests to implementation, so they break on refactors that changed nothing real.