← Back to context

Comment by vkou

3 months ago

All rules exist to be broken in the right circumstances. But in 99.9% of test code, there's no reason to do any of that.

I think when testing code with an open call, it is a good idea to test what happens on different return values of open. If that is not what you intent to test for this method, then that method shouldn't contain open at all, as already pointed out by other comments.

  • That depends on what your error recovery plan is.

    If the code's running in a space shuttle, you probably want to test that path.

    If it's bootstrapping a replicated service, it's likely desirable to crash early if a config file couldn't be opened.

    If it's plausible that the file in question is missing, you can absolutely test that code path, without mocking open.

    If you want to explicitly handle different reasons for why opening a file failed differently, by all means, stress all of that in your tests. But if all you have is a happy path and an unhappy path, where your code doesn't care why opening a file failed, all you need to test is the case where the file is present, and one where it is not.

    • Modifying the file system to be would be kind of like mocking to me. I very much, don't want my daemons or user-facing applications to just crash, when a file is missing. That's kind-of the worst thing you can do.

      1 reply →