logoalt Hacker News

vkouyesterday at 10:49 AM1 replyview on HN

> This blog post talks as if mocking the `open` function is a good thing that people should be told how to do.

It does. And this is exactly the problem, here!

> TFA: The thing we want to avoid is opening a real file

No! No, no, no! You do not 'want to avoid opening a real file' in a test.

It's completely fine to open a real file in a test! If your code depends on reading input files, then your test should include real input files in it! There's no reason to mock any of this. All of this stuff is easy to set up in any unit test library worth it's salt.


Replies

9rxyesterday at 3:47 PM

> then your test should include real input files in it! There's no reason to mock any of this.

That's okay for testing some branches of your code. But not all. I don't want to have to actually crash my hard drive to test that I am properly handling hard drive crashes. Mocking[1] is the easiest way to do that.

[1] For some definition of mock. There is absolutely no agreement found in this space as to what the terms used mean.