i try this simple test to work with FileSystemWatcher Class but the OnFileChanged event not firing
[Test]
public void CanNotifyWhenFileChanged()
{
var watcher = new XMLWatcher(_path);
watcher.StartWatching();
AddXMLNodeTofile(_path);// change the file
bool c = watcher.IsFileChanged;
Assert.IsTrue(c);
}
and this is my Method
public void StartWatching()
{
var watcher = new FileSystemWatcher
{
Path =
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location),
IncludeSubdirectories = false,
NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite |
NotifyFilters.FileName |
NotifyFilters.DirectoryName,
Filter = FilePath
};
watcher.Changed += OnFileChanged;
watcher.EnableRaisingEvents = true;
}
Please note that some Unit Test runners (NUnit, MSTest) do shadow copying of files i.e. the test binaries are copied to a separate location before execution. Hence the _path and the path where your test assembly reside may not be the same.
Also make sure that _path and the folder you are watching are the same.