こんにちは、現在、構成ファイルへの変更の失敗をテストしようとしています。
これを行うには、次のコードを使用します...
string path = _pathInvalidFormat.Substring(0, _pathInvalidFormat.LastIndexOf('\\'));
System.IO.File.Copy("TestInvalidXmlConfigurationFile.xml", _pathInvalidFormat, true);
FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(path);
//catch the invalid file getting deleted
fileSystemWatcher.Deleted += new FileSystemEventHandler(fileSystemWatcher_Deleted);
//catch the temporary config file getting renamed
fileSystemWatcher.Renamed += new RenamedEventHandler(fileSystemWatcher_Renamed);
fileSystemWatcher.EnableRaisingEvents = true;
CreateConfig(_pathInvalidFormat);
System.Threading.Thread.Sleep(5000);
Assert.That(_oldFileDeleted);
Assert.That(_newFileCreated);
ResetConfig(_pathInvalidFormat);
ただし、この System.Threading.Thread.Sleep(5000); の使用には満足していません。
fileSystemWatcher.WaitForChanged(WatcherChangeTypes.Deleted, 5000); を使用してみました。しかし、常にタイムアウトに達しているように見えるため、機能するようには見えません。Deleted イベントが発生したことを確認できても、まだ戻りません。
あるいは、非同期イベントが発生するのをシステムに待機させるより良い方法はありますか?
ありがとう
キーラン
-- 編集:
次の方法で createconfig(path) を使用してシステム構成ファイルを作成します
private void ReadConfiguration(string path)
{
var configFileMap = new ExeConfigurationFileMap()
{
ExeConfigFilename = path,
};
// if we try to read bad formatted file let's
// 1. delete this file
// 2. call read configuration to form correct file
try
{
Config = System.Configuration.ConfigurationManager
.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
}
catch (ConfigurationErrorsException)
{
// ok, there is bad format file, let delete it and create another one
// TODO: check security rights?
File.Delete(path);
ReadConfiguration(path);
}
}
これにより、私のテストが何を達成しようとしているのかについての洞察が得られることを願っています。