简体   繁体   English

单元测试FileSystemWatcher.Error事件

[英]Unit-test FileSystemWatcher.Error Event

I'm trying to unit test a private method that I have attached to my FileSystemWatcher's Error event. 我正在尝试对附加到FileSystemWatcher的Error事件的私有方法进行单元测试。 MSDN says that this event "occurs when the internal buffer overflows." MSDN说此事件“在内部缓冲区溢出时发生”。 I've tried to cause a buffer overflow but have not been successful so far. 我试图导致缓冲区溢出,但到目前为止尚未成功。 The FileSystemWatcher's various properties are: FileSystemWatcher的各种属性是:

fileWatcher.IncludeSubdirectories = false;
fileWatcher.Filter = "*";
fileWatcher.NotifyFilter = (NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.Size);

What is the best way of raising this event for the purpose of unit-testing? 为了进行单元测试,引发此事件的最佳方法是什么?

I doubt you will be able to reliably cause a buffer overflow. 我怀疑您是否能够可靠地导致缓冲区溢出。 It will be system and configuration dependent. 这将取决于系统和配置。 (It may even be load dependent). (它甚至可能取决于负载)。 I would suggest that instead you mock the FileSystemWatcher class and trigger the mocked event yourself to test your handler. 我建议您改为模拟FileSystemWatcher类,并自己触发模拟事件以测试处理程序。

See Wikipedia: Mock Object and this List of Mocking frameworks for some more details if you're new to the idea of mocking. 如果您不熟悉 模拟的想法,请参阅Wikipedia:Mock Object和此Mocking框架列表以获取更多详细信息。

Just added a handler for the Error event to an existing code :). 刚刚将Error事件的处理程序添加到现有代码中:)。 I was testing the processing of the files and with 112 files (as I have from the bug i'm working on) this gets triggered each time I copy the files to the watched folder. 我正在测试文件的处理,使用112个文件(正如我正在研究的bug所做的那样),每次将文件复制到监视的文件夹时都会触发该文件。 Each file is about 72k and I copy them using TotalCommander which I suppose does a plain iterative copy. 每个文件约为72k,我使用TotalCommander复制它们,我想做一个普通的迭代复制。

Unfortunately for you it all depends on the sys config & load (as simon pointed out). 不幸的是,这一切都取决于系统配置和负载(如simon所指出的)。

Sure you can try to flush after each written byte with eventually a number of writers, but nothing can guaranty it. 当然,您可以尝试在每个写入的字节之后最终与许多作家一起冲洗,但是没有什么可以保证的。

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

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