Search code examples
c#.netunit-testingfilesystemwatcher

Unit-test FileSystemWatcher.Error Event


I'm trying to unit test a private method that I have attached to my FileSystemWatcher's Error event. MSDN says that this event "occurs when the internal buffer overflows." I've tried to cause a buffer overflow but have not been successful so far. The FileSystemWatcher's various properties are:

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?


Solution

  • 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.

    See Wikipedia: Mock Object and this List of Mocking frameworks for some more details if you're new to the idea of mocking.