http://qs321.pair.com?node_id=11121966


in reply to Re^2: Can Test::MockObject mock a file?
in thread Can Test::MockObject mock a file?

I did not like the idea of putting files for these tests on a user's filesystem.

Think of it this way; it's difficult to anticipate all of the ways user filesystems might differ from your expectations. One might be encrypted. Another might be case-insensitive. A third might be on a slow network mount.

By testing as much of that system as possible—by not mocking things—your tests are richer and better represent the conditions you want to validate.

Using File::Temp avoids at least two difficult problems. First, it reduces your need to clean up after yourself by handling it for you. Hopefully that eases your mind about writing to user filesystems. Second, it makes your tests less serial; you can run them in parallel because there's very little chance multiple tests or multiple runs will collide because they all depend on the state of one specific file with a hard-coded name and path.

If you wrap your temporary file generation in a function, it could be even cleaner. The third time you write to a temporary file, I'd do that; you'll reduce duplication and further reduce coupling on the generated name of the temporary file.

Replies are listed 'Best First'.
Re^4: Can Test::MockObject mock a file?
by Lady_Aleena (Priest) on Sep 21, 2020 at 20:06 UTC

    I am sorry I am having a hard time figuring out why I would want to put the file generation into a function. Each "file" generated is different, and they do not take up that many lines in the test. Not much is being duplicated as far as I can see.

    My OS is Debian 10 (Buster); my perl versions are 5.28.1 local and 5.16.3 or 5.30.0 on web host depending on the shebang.

    No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
    Lady Aleena
      Not much is being duplicated as far as I can see.

      Not a huge amount, no, but when you start putting encoding there, you may find that modifying all the places you generate these files is more work than modifying it in one place.

      Each "file" generated is different

      Sure, but creating the filehandle, writing it out, closing it, checking for errors, all of that is the same.

      Like I said, I wouldn't worry about it until your test file gets much larger. It's just a matter of taste at this point.

        ... modifying all the places you generate these files is more work than modifying it in one place.

        Lady_Aleena: Remember: DRY!


        Give a man a fish:  <%-{-{-{-<