Nea*_*ear 7 .net c# unit-testing filesystemwatcher
我FileSystemWatcher看了一个目录进行更改,当有一个新的XML文件时,它会解析该文件并对其执行某些操作.
我在我的项目中有一些示例XML文件,我用于我编写的解析器的单元测试目的.
我正在寻找一种方法来使用示例XML文件来测试FileSystemWatcher.
是否可以以编程方式创建事件(以某种方式涉及XML文件)以触发FSW.Changed事件?
Kon*_*man 11
我认为你在这里采取了错误的做法.
你不应该尝试直接对该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; }
}
}
Run Code Online (Sandbox Code Playgroud)
(注意如何将实例FileSystemWatcher传递给类构造函数;您可以在构造包装器时动态创建新实例)
2)提取类的接口:
public interface IFileSystemWatcherWrapper
{
event FileSystemEventHandler Changed;
bool EnableRaisingEvents { get; set; }
}
//and therefore...
public class FileSystemWatcherWrapper : IFileSystemWatcherWrapper
Run Code Online (Sandbox Code Playgroud)
3)使你的类依赖于接口:
public class TheClassThatActsOnFilesystemChanges
{
private readonly IFileSystemWatcherWrapper fileSystemWatcher;
public TheClassThatActsOnFilesystemChanges(IFileSystemWatcherWrapper fileSystemWatcher)
{
this.fileSystemWatcher = fileSystemWatcher;
fileSystemWatcher.Changed += (sender, args) =>
{
//Do something...
};
}
}
Run Code Online (Sandbox Code Playgroud)
4)在应用程序初始化时,使用任何依赖注入引擎实例化您的类,或者只是做穷人的注入:
var theClass = new TheClassThatActsOnFilesystemChanges(
new FileSystemWatcherWrapper(new FileSystemWatcher()));
Run Code Online (Sandbox Code Playgroud)
5)现在继续编写单元测试,TheClassThatActsOnFilesystemChanges通过随意创建一个模拟IFileSystemWatcherWrapper火灾事件!您可以使用任何模拟引擎,例如Moq.
底线:
如果您依赖于您无法控制的类和/或无法进行有意义的单元测试,请使用适当的接口编写环绕,并依赖于接口.你的包装器很薄,如果你不能对它进行单元测试就不会真正受到伤害,而你的客户端类现在可以进行适当的单元测试.
| 归档时间: |
|
| 查看次数: |
2946 次 |
| 最近记录: |