开发者

Mock filesystem for Perl unit testing

Is there a method or module that allows me to simulate a fake filesystem for use with Perl unit testing?

I use a variety of Test::* and Test::Mock* modules in my unit testing, but occasionally I'd like to be able to fake a filesystem. A simple example (and a minimum requirement) would be faking the result from the -r or -f file test operators, so that when I call objects or modules that perform file tests, I can more easily control the result.

I know that there are all manner of ways of getting around this problem, such as creating temporary files or wrapping file tests in their own开发者_Go百科 functions which can then be mocked, but sometimes it would be nice if I could just get Perl to lie to me... So solutions that suggest rewriting the code being tested are not required ;-)

Update:

After having just found and read this SO question, rewriting the code so that it is more testable is looking like the more sensible option. Still, fingers crossed...


These are worth a look: Filesys::Virtual and Filesys::Virtual::Async. I’ve used the first. Mixed with Path::Class and File::Temp it is easy to do all kinds of self-cleaning things tersely and elegantly. Test::Virtual::Filesystem also looks interesting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜