public async Task TestCreatedAndRenamedEvents() { var fileSystemWatcherWrapperMock = new Mock<IFileSystemWatcher>(); var pathServiceMock = new Mock<IPathService>(); pathServiceMock .Setup(m => m.GetParentDirectory(It.IsAny<string>())) .Returns(DirectoryPath); var configuration = GetConfiguration(); var decorator = new AggregatingFileSystemWatcherDecorator(pathServiceMock.Object, fileSystemWatcherWrapperMock.Object, configuration); var actualCallsCount = 0; var isCallbackCalled = false; decorator.Created += (sender, args) => { Assert.Equal(WatcherChangeTypes.Created, args.ChangeType); Assert.Equal(NewFileName, args.Name); actualCallsCount++; }; decorator.Renamed += (sender, args) => isCallbackCalled = false; var createdArgs = new FileSystemEventArgs(WatcherChangeTypes.Created, DirectoryPath, FileName); fileSystemWatcherWrapperMock.Raise(m => m.Created += null, createdArgs); var renamedArgs = new RenamedEventArgs(WatcherChangeTypes.Renamed, DirectoryPath, NewFileName, FileName); fileSystemWatcherWrapperMock.Raise(m => m.Renamed += null, renamedArgs); await Task.Delay(DelayIntervalMs); Assert.Equal(1, actualCallsCount); Assert.False(isCallbackCalled); }
public async Task TestChangedEvents() { var fileSystemWatcherWrapperMock = new Mock<IFileSystemWatcher>(); var pathServiceMock = new Mock<IPathService>(); var configuration = GetConfiguration(); var decorator = new AggregatingFileSystemWatcherDecorator(pathServiceMock.Object, fileSystemWatcherWrapperMock.Object, configuration); var actualCallsCount = 0; decorator.Changed += (sender, args) => { Assert.Equal(WatcherChangeTypes.Changed, args.ChangeType); Assert.Equal(FileName, args.Name); actualCallsCount++; }; for (var i = 0; i < 10; i++) { var changedArgs = new FileSystemEventArgs(WatcherChangeTypes.Changed, DirectoryPath, FileName); fileSystemWatcherWrapperMock.Raise(m => m.Changed += null, changedArgs); } await Task.Delay(DelayIntervalMs); Assert.Equal(1, actualCallsCount); }
public async Task TestCreatedAndDeletedEvents() { var fileSystemWatcherWrapperMock = new Mock <IFileSystemWatcher>(); var pathServiceMock = new Mock <IPathService>(); var configuration = GetConfiguration(); var decorator = new AggregatingFileSystemWatcherDecorator(pathServiceMock.Object, fileSystemWatcherWrapperMock.Object, configuration); var isCallbackCalled = false; decorator.Created += (sender, args) => isCallbackCalled = true; decorator.Deleted += (sender, args) => isCallbackCalled = true; var createdArgs = new FileSystemEventArgs(WatcherChangeTypes.Created, DirectoryPath, FileName); fileSystemWatcherWrapperMock.Raise(m => m.Created += null, createdArgs); var changedArgs = new FileSystemEventArgs(WatcherChangeTypes.Deleted, DirectoryPath, FileName); fileSystemWatcherWrapperMock.Raise(m => m.Deleted += null, changedArgs); await Task.Delay(DelayIntervalMs); Assert.False(isCallbackCalled); }
public async Task TestCleanup() { var fileSystemWatcherWrapperMock = new Mock <IFileSystemWatcher>(); fileSystemWatcherWrapperMock .Setup(m => m.Dispose()) .Verifiable(); fileSystemWatcherWrapperMock .Setup(m => m.StopRaisingEvents()) .Verifiable(); var pathServiceMock = new Mock <IPathService>(); var configuration = GetConfiguration(); var decorator = new AggregatingFileSystemWatcherDecorator(pathServiceMock.Object, fileSystemWatcherWrapperMock.Object, configuration); var isCallbackCalled = false; decorator.Changed += (sender, args) => isCallbackCalled = true; decorator.StopRaisingEvents(); decorator.Dispose(); for (var i = 0; i < 10; i++) { var changedArgs = new FileSystemEventArgs(WatcherChangeTypes.Changed, DirectoryPath, FileName); fileSystemWatcherWrapperMock.Raise(m => m.Changed += null, changedArgs); } await Task.Delay(DelayIntervalMs); Assert.False(isCallbackCalled); fileSystemWatcherWrapperMock.Verify(m => m.StopRaisingEvents(), Times.Once); fileSystemWatcherWrapperMock.Verify(m => m.Dispose(), Times.Once); }