テストしたい次のコードがあります。
public class DirectoryProcessor
{
public string DirectoryPath
{
get;
set;
}
private FileSystemWatcher watcher;
public event EventHandler<SourceEventArgs> SourceFileChanged;
protected virtual void OnSourceFileChanged(SourceEventArgs e)
{
EventHandler<SourceEventArgs> handler = SourceFileChanged;
if(handler != null)
{
handler(this, e);
}
}
public DirectoryProcessor(string directoryPath)
{
this.DirectoryPath = directoryPath;
this.watcher = new FileSystemWatcher(directoryPath);
this.watcher.Created += new FileSystemEventHandler(Created);
}
void Created(object sender, FileSystemEventArgs e)
{
// process the newly created file
// then raise my own event indicating that processing is done
OnSourceFileChanged(new SourceEventArgs(e.Name));
}
}
基本的に、私は次のことを行うNUnitテストを書きたいと思います。
- ディレクトリを作成する
- セットアップ
DirectoryProcessor
- いくつかのファイルをディレクトリに書き込みます(経由
File.WriteAllText()
) DirectoryProcessor.SourceFileChanged
手順3で追加したファイルごとに1回起動したことを確認します。
これを実行して手順3の後に追加しようとしThread.Sleep()
ましたが、タイムアウトを正しく取得するのは困難です。ディレクトリに書き込んだ最初のファイルは正しく処理されますが、2番目のファイルは処理されません(タイムアウトが60秒に設定されています)。このように動作させることができたとしても、テストを書くのはひどい方法のようです。
誰かがこの問題に対する良い解決策を持っていますか?