Repository navigation
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);
}
Activity
issue-label-bot commentedon Nov 27, 2020
Issue-Label Bot is automatically applying the label
bugto this issue, with a confidence of 0.93. Please mark this comment with 👍 or 👎 to give our bot feedback!Links: app homepage, dashboard and code for this bot.
fgreinacher commentedon Nov 28, 2020
Thanks for writing this issue!
The behavior you're describing is by-design since #409.
You're changes look alright to me!
Feel free to reopen this issue if anything is unclear to you.
chandrasoma-senani123 commentedon Nov 28, 2020
Thanks for your reply. Could you add an example of MockFileSystemWatcherFactory usage please?
fgreinacher commentedon Nov 30, 2020
Sure, take a look at https://github.com/System-IO-Abstractions/System.IO.Abstractions/blob/65de5f117a899c8b358f6d9b57e8c71d103fe353/tests/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs#L376
chandrasoma-senani123 commentedon Nov 30, 2020