You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
I'd like to be able to test code dependent on IFileSystemWatcher. Currently get a NotImplementedException when using MockFileSystem.FileSystemWatcher.
Describe the solution you'd like
An implementation of MockFileSystem.FileSystemWatcher that will fire appropriate events when files/directories are modified via MockFileSystem.File and MockFileSystem.Directory.
Ah, okay, thank you. I have tried deriving from MockFileSystem and overriding the FileSystemWatcher property but I can't see any way to intercept calls to IMockFileDataAccessor to support the functionality of my file watcher mock (e.g. when a file is removed, the watcher will notify). I think I would need to reimplement MockFileSystem to add this behaviour unless you know of something else?
@fgreinacher I am curious if there is a middle ground between trying to ship a fully functioning MockFileSystemWatcher and forcing the consumer to provide one just to allow their tests to run at all.
For context: I have a class that accepts IFileSystem as an injected dependency. That code uses IFileSystemWatcher as well as various other IFileSystem APIs. I have two testing desires:
Test APIs that are not reliant on the file system watching aspect at all
Test behaviors that are triggered by the IFileSystemWatcher events
But because we set up a file watcher at construction, the built-in MockFileSystem throws even for the APIs that don't use it.
In order to test my non-watching behaviors, I have to provide some sort of IFileSystemFactory implementation. In order to test the IFileSystemWatcher event handlers, I'd obviously need to build that capability into my custom factory/watcher.
The first piece is rather trivial, and I haven't gotten to the second piece yet.
While I absolutely agree that we don't want the default behavior to be misleading/incomplete, it seems like we could provide a more useful opt-in option out of the box too.
Would you be open to either of the below ideas?
Ship a NoOpFileSystemWatcher that allows the dependent code to set up the file watcher and access any events/properties without any errors, but makes no attempts to actually trigger any events
Ship a ManuallyTriggeredFileSystemWatcher that allows setup as above, but that also allows the test code to manually trigger specific events
This avoids the need to deal with replicating exact file-watching behaviors while still providing an option for testing the event handlers
I admittedly haven't attempted to actually develop either of these yet, but my gut instinct is that we'd be able to keep complexity fairly low. I'd be willing to take a stab at it if you are not opposed.
@zivarah : I created an alternative MockFileSystem in Testably.Abstractions, that uses the same interface as System.IO.Abstractions and which implements a fully-functional FileSystemWatcher.
You can directly use this mocked filesystem in your tests without having to change your production code.
A very basic example test (deletion of a file) that illustrates the functionality would be:
@zivarah Yes, absolutely! Both options seem fine to me, as long as we keep them opt-in. Feel free to submit a PR for discussing how we can integrate this nicely!
Activity
fgreinacher commentedon May 27, 2022
Thanks for writing the issue!
We already discussed this and came to the conclusion that we don't want it in the core library. My suggestion here would be to use the existing extensibility points and provide your own implementation.
CallumECameron commentedon Jun 6, 2022
Ah, okay, thank you. I have tried deriving from MockFileSystem and overriding the FileSystemWatcher property but I can't see any way to intercept calls to IMockFileDataAccessor to support the functionality of my file watcher mock (e.g. when a file is removed, the watcher will notify). I think I would need to reimplement MockFileSystem to add this behaviour unless you know of something else?
fgreinacher commentedon Jun 23, 2022
Sorry @CallumECameron, I forgot to get back to this after my vacation.
At the moment I see two options:
IFileSystemthat intercepts calls and triggers the expected events. There is a good example at Make the test library open for extension #597 (comment).Does that help?
CallumECameron commentedon Jul 5, 2022
No problem, @fgreinacher, thanks for getting back to me. This helped, thank you.
zivarah commentedon May 14, 2023
@fgreinacher I am curious if there is a middle ground between trying to ship a fully functioning MockFileSystemWatcher and forcing the consumer to provide one just to allow their tests to run at all.
For context: I have a class that accepts IFileSystem as an injected dependency. That code uses IFileSystemWatcher as well as various other IFileSystem APIs. I have two testing desires:
But because we set up a file watcher at construction, the built-in MockFileSystem throws even for the APIs that don't use it.
In order to test my non-watching behaviors, I have to provide some sort of IFileSystemFactory implementation. In order to test the IFileSystemWatcher event handlers, I'd obviously need to build that capability into my custom factory/watcher.
The first piece is rather trivial, and I haven't gotten to the second piece yet.
While I absolutely agree that we don't want the default behavior to be misleading/incomplete, it seems like we could provide a more useful opt-in option out of the box too.
Would you be open to either of the below ideas?
I admittedly haven't attempted to actually develop either of these yet, but my gut instinct is that we'd be able to keep complexity fairly low. I'd be willing to take a stab at it if you are not opposed.
vbreuss commentedon May 16, 2023
@zivarah : I created an alternative MockFileSystem in Testably.Abstractions, that uses the same interface as System.IO.Abstractions and which implements a fully-functional FileSystemWatcher.

You can directly use this mocked filesystem in your tests without having to change your production code.
A very basic example test (deletion of a file) that illustrates the functionality would be:
fgreinacher commentedon May 20, 2023
@zivarah Yes, absolutely! Both options seem fine to me, as long as we keep them opt-in. Feel free to submit a PR for discussing how we can integrate this nicely!