如何对 FileSystemWatcher 事件进行单元测试?
我正在开发一个处理转储到目标目录中的文档的系统。 我通过检测 FileSystemWatcher OnChanged 事件来获取文件。
作为单元测试的一部分,我应该如何自动化它? 我应该如何组织比较的输入文件和输出文件以验证转换是否正确? 通过发出 diff 命令的批处理文件可以更好地处理这种情况吗?
I am working on a system that processes documents that are dumped into a target directory. I pick up the files by detecting the FileSystemWatcher OnChanged event.
As part of a unit test, how should I go about automating this? How should I organize the input files and the output files that get compared to verify that the transformations are correct? Is this something that is better handled by a batch file issuing diff commands?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,首先考虑一下您到底想要测试什么。
是否在检测到文件发生更改后,就对其进行了正确处理?
或者 FileSystemWatcher 类是否有效?
或者操作系统是否以正确的顺序向您发送正确的通知(其中正确基本上意味着如您所期望的)?
如果只需要完成处理,我会考虑模拟观察者类,或者使用 IoC 容器为您提供为单元测试触发事件的东西。
如果操作系统是否以正确的顺序提供正确的事件,那么我会模拟处理该文件的代码,只是为了保存“是的,我被告知该文件已更改”的状态,以及单元测试然后将操作临时目录中的文件。
但请注意,单元测试依赖的外部复杂性越多,它就越容易被破坏。
Well, first consider what exactly it is that you want to test.
Is it that after you've detected that a file has change, it is processed correctly?
Or is it that the FileSystemWatcher class works?
Or is it that the operating system sends you the right notifications in the right order (where right basically means as you expect it)?
If only the processing has to be done, I would consider mocking up the watcher class, or use an IoC container to provide you with something that fires events for the unit test.
If it's wether the operating system provides the right events in the right order, then I would mock up the code that processes the file, just to save state that says "yes, I was told that this file had changed", and the unit test would then manipulate files in a temp-directory.
But be warned, the more outside complexity your unit test relies on, the more breakable it will be.