-
Notifications
You must be signed in to change notification settings - Fork 267
Description
I have used your new MockFileSystemWatcherFactory class in my UNIT testing and I get the following exception:
Expected: No Exception to be thrown
But was: <System.NotImplementedException: MockFileSystem does not have a built-in FileSystemWatcher implementation. You must provide your own mock or implementation of IFileSystemWatcherFactory and assign it to MockFileSystem.FileSystemWatcher.
at System.IO.Abstractions.TestingHelpers.MockFileSystemWatcherFactory.CreateNew(String path, String filter)
at Tests.Services.FileSystemWatcherTests.<>c__DisplayClass2_0.<FileSystemWatcher_Should_Create_Watcher_Successfully>b__0() in C:\Services\FileSystemWatcherTests.cs:line 36
--- End of stack trace from previous location ---
at NUnit.Framework.Internal.ExceptionHelper.Rethrow(Exception exception)
at NUnit.Framework.Internal.Reflect.DynamicInvokeWithTransparentExceptions(Delegate delegate)
at NUnit.Framework.Internal.ExceptionHelper.RecordException(Delegate parameterlessDelegate, String parameterName)>
at Tests.Services.FileSystemWatcherTests.FileSystemWatcher_Should_Create_Watcher_Successfully() in C:\Services\FileSystemWatcherTests.cs:line 36
Here is my UNIT test:
[TestFixture]
public class FileSystemWatcherTests
{
private IServiceCollection _services;
[SetUp]
public void Setup()
{
_services = new ServiceCollection();
var fileSystemWatcherFactory = new MockFileSystemWatcherFactory();
_services.AddSingleton<IFileSystemWatcherFactory>(sp => fileSystemWatcherFactory);
}
[Test]
public void FileSystemWatcher_Should_Create_Watcher_Successfully()
{
var serviceProvider = _services.BuildServiceProvider();
var fileSystemWatcherFactory = serviceProvider.GetRequiredService<IFileSystemWatcherFactory>();
Assert.DoesNotThrow(() => fileSystemWatcherFactory.CreateNew(@"C:\Some\Directory", "*.txt"));
}
}
To get it working I have changed the Setup() method as follows:
[SetUp]
public void Setup()
{
_services = new ServiceCollection();
var fileSystemWatcherFactory = new Mock<IFileSystemWatcherFactory>();
fileSystemWatcherFactory.Setup(x => x.CreateNew(It.IsAny<string>(), It.IsAny<string>()))
.Returns(Mock.Of<IFileSystemWatcher>());
_services.AddSingleton<IFileSystemWatcherFactory>(sp => fileSystemWatcherFactory.Object);
}