简体   繁体   English

Perl 单元测试的模拟文件系统

[英]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?有没有一种方法或模块可以让我模拟一个假文件系统以用于 Perl 单元测试?

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.我在单元测试中使用了各种Test::*Test::Mock*模块,但偶尔我希望能够伪造文件系统。 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.一个简单的例子(也是最低要求)是伪造来自-r-f文件测试运算符的结果,这样当我调用执行文件测试的对象或模块时,我可以更轻松地控制结果。

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 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;-)我知道有很多方法可以解决这个问题,例如创建临时文件或将文件测试包装在他们自己的函数中,然后可以模拟,但有时如果我能让 Perl 对我撒谎就好了...因此不需要建议重写正在测试的代码的解决方案;-)

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.在刚刚找到并阅读了这个 SO question之后,重写代码以使其更具可测试性看起来是更明智的选择。 Still, fingers crossed...尽管如此,手指交叉...

These are worth a look: Filesys::Virtual and Filesys::Virtual::Async .这些值得一看: Filesys::VirtualFilesys::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.Path::ClassFile::Temp混合使用,可以轻松优雅地完成各种自清洁工作。 Test::Virtual::Filesystem also looks interesting. Test::Virtual::Filesystem看起来也很有趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM