Skip to content

New MockFileSystemWatcherFactory class throws error #681

Closed

Description

@chandrasoma-senani123

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

  1. issue-label-bot commented on Nov 27, 2020

    @issue-label-bot

    Issue-Label Bot is automatically applying the label bug to 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.

  2. fgreinacher commented on Nov 28, 2020

    @fgreinacher
    Contributor

    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.

  3. chandrasoma-senani123 commented on Nov 28, 2020

    @chandrasoma-senani123
    Author

    Thanks for your reply. Could you add an example of MockFileSystemWatcherFactory usage please?

  4. chandrasoma-senani123 commented on Nov 30, 2020

    @chandrasoma-senani123
    Author
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    state: needs discussionIssues that need further discussiontype: bugIssues that describe misbehaving functionality

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions