Testing for Failure
Learn how to do failure-path testing in unit tests.
We'll cover the following
Failure is always a possibility, so we need to test for it. We prefer to do failure-path testing in unit tests rather than end-to-end tests. Success requires the entire system to work in concert. A failure response can usually be isolated to one component. That said, it’s often useful to have one end-to-end failure test, especially in cases where the failure is easily usable and visible to a typical user.
We haven’t yet learned about the tools that will allow us to fake failure. That requires a mock object package (see Using Test Doubles as Mocks and Stubs). But in this case, it’s not hard to create a real failure by adding a validation that we can then not fulfill.
Let’s first see what an end-to-end failure test might look like by illustrating what would happen if we allow a user to create a project without a name: