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.