【问题标题】:Unit-testing FileSystemWatcher: How to programatically fire a changed event?单元测试 FileSystemWatcher:如何以编程方式触发更改的事件?
【发布时间】:2018-06-20 20:26:54
【问题描述】:

我有一个FileSystemWatcher 监视目录的更改,当其中有一个新的 XML 文件时,它会解析该文件并对其进行处理。

我的项目中有一些示例 XML 文件,用于对我编写的解析器进行单元测试。

我正在寻找一种方法来使用示例 XML 文件来测试 FileSystemWatcher。

是否可以以编程方式创建事件(以某种方式涉及 XML 文件)以触发 FSW.Changed 事件?

【问题讨论】:

  • 你不能打开文件并在 xml 文件中添加一些测试,例如添加评论,然后她将其返回到磁盘
  • 请分享您要测试并尝试进行单元测试的 FSW 代码
  • 首先,您可以为 FileSystemWatcher 创建一个模拟对象并将其传递给您的类,然后调用模拟的更改事件。另一种选择是您可以创建一个从 FileSystemWatcher 派生的模拟类,并根据需要覆盖更改的方法并将其传递给您的类进行测试。另一种选择是,在您的测试中,当您的代码正在监视文件更改时,在并行任务中将真实文件复制并粘贴到您的目录中。
  • @mecek 你如何调用 mock 的 changed 事件?

标签: c# .net unit-testing filesystemwatcher


【解决方案1】:

我认为你在这里采取了错误的方法。

您不应该尝试直接对FileSystemWatcher 类进行单元测试(您不能——您无法控制它!)。相反,您可以尝试以下方法:

1) 为FileSystemWatcher 类编写一个仅 将其功能委托给FileSystemWatcher 实例的包装类。下面是一个方法一个事件的例子,根据需要添加更多成员:

public class FileSystemWatcherWrapper
{
    private readonly FileSystemWatcher watcher;

    public event FileSystemEventHandler Changed;

    public FileSystemWatcherWrapper(FileSystemWatcher watcher)
    {
        this.watcher = watcher
        watcher.Changed += this.Changed;
    }

    public bool EnableRaisingEvents
    {
        get { return watcher.EnableRaisingEvents; }
        set { watcher.EnableRaisingEvents = value; }
    }
}

(注意FileSystemWatcher 的实例是如何传递给类构造函数的;您可以在构造包装器时动态创建一个新实例)

2) 提取类的接口:

public interface IFileSystemWatcherWrapper
{
    event FileSystemEventHandler Changed;
    bool EnableRaisingEvents { get; set; }
}

//and therefore...

public class FileSystemWatcherWrapper : IFileSystemWatcherWrapper

3) 让你的类依赖于接口:

public class TheClassThatActsOnFilesystemChanges
{
    private readonly IFileSystemWatcherWrapper fileSystemWatcher;

    public TheClassThatActsOnFilesystemChanges(IFileSystemWatcherWrapper fileSystemWatcher)
    {
        this.fileSystemWatcher = fileSystemWatcher;

        fileSystemWatcher.Changed += (sender, args) =>
        {
            //Do something...
        };
    }
}

4) 在应用程序初始化时,使用任何依赖注入引擎来实例化你的类,或者只是做穷人的注入:

var theClass = new TheClassThatActsOnFilesystemChanges(
    new FileSystemWatcherWrapper(new FileSystemWatcher()));

5) 现在继续为TheClassThatActsOnFilesystemChanges 编写单元测试,方法是创建一个IFileSystemWatcherWrapper 的模拟,它可以随意触发事件!您可以为此使用任何模拟引擎,例如Moq。

底线:

当您依赖于您无法控制和/或无法进行有意义的单元测试的类时,请使用适当的接口对其进行包装,并依赖于该接口。您的包装器非常薄,如果您不能对其进行单元测试,它不会真正受到伤害,而您的客户端类现在可以正确地进行单元测试。

【讨论】:

  • FSW 是一个很难准确模拟的野兽。确保在您的测试计划中包括延迟、重复通知、来自其他软件的锁定和丢失通知。我可能还缺少其他警告。
  • 您是否在示例中链接事件?我似乎无法让事件像那样连锁。有什么我可能遗漏的吗?
  • 谢谢!请修正小错字 - 添加“this.watcher = watcher;”在 FileSystemWatcherWrapper 构造函数中。
  • @BluE 可以将事件触发的公共方法添加到FileSystemWatcherWrapper。
【解决方案2】:

只需从FileSystemWatcher 派生并将您需要的任何内容添加到界面:

public interface IFileSystemWatcherWrapper : IDisposable
{        
    bool EnableRaisingEvents { get; set; }
    event FileSystemEventHandler Changed;
    //...
}

public class FileSystemWatcherWrapper : FileSystemWatcher, IFileSystemWatcherWrapper
{
    public FileSystemWatcherWrapper(string path, string filter)
        : base(path, filter)
    {
    }
}

【讨论】:

    【解决方案3】:

    由于FileSystemWatcher不是一个密封类,你可以继承它并创建一个接口:

    public class FileSystemWatcherWrapper : FileSystemWatcher, IFileSystemWatcherWrapper
        {
          //empty on purpose, doesnt need any code
        }
    
     public interface IFileSystemWatcherWrapper
        {
            event FileSystemEventHandler Created;
            event FileSystemEventHandler Deleted;
            bool EnableRaisingEvents { get; set; }
    
            bool IncludeSubdirectories { get; set; }
    
            string Path { get; set; }
            string Filter { get; set; }
    
            void Dispose();
        }
    }
    

    然后您可以使用 Moq by Mock 进行单元测试

    【讨论】:

    • 使用 System.IO.Abstractions.IFileSystemWatcher 接口编写单元测试,我只需要为 DI 创建 FileSystemWatcherWrapper 类就可以了。非常简洁且低代码的解决方案。
    猜你喜欢
    • 2015-10-09
    • 2010-11-15
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多