← Back to context

Comment by time0ut

12 days ago

Maybe I am missing something, but how else would I test various exception handling paths?

There is a whole world of errors that can occur during IO. What happens if I get a 500 from that web service call? How does my code handle a timeout? What if the file isn't found?

It is often only possible to simulate these scenarios using a mock or similar. These are also code paths you really want to understand.

Put a small data interface around your IO, have it return DATA | NOT_FOUND etc.

Then your tests don't need behavioral mocks or DI, they just need the different shapes of data and you test your own code instead of whatever your IO dependency is or some simulation thereof.

  • Sure. This is a good practice for multiple reasons. However, the code that glues my interface to the underlying I/O is still there and needs testing, right?

    I agree with you in general. But it always feels like there are spots where a mock of some kind is the only way to cover certain things.

  • isn't that just mocking with extra steps?

    • You could call the data you generate for the tests "mocks".

      But they really aren't "mocks" in the sense of behavioral mocks via IoC/DI and you don't need to manipulate them via some kind of interface in order to put them into the right state for your particular tests.

      There are some extra steps, but you get extremely simple and reliable tests in return.

      In many(!) cases you already have a data interface, especially with HTTP/REST APIs. All you need to do is simply not bury the IO call down the stack and maybe describe the failure conditions as plain data in your signature and voila.

      (This is not a replacement for higher order testing like, manual, E2E or integration tests. But it certainly beats unit testing with mocks IMO.)

I don't think there's a disagreement; the author states "Whenever I look at mocks, they mostly have the same problem as all unit tests that I see, they only model the happy path". So by corollary their opinion of the correct usage of mocking would also include modelling brokenness.