diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs index 34fc81400..513460d3b 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; @@ -304,5 +305,39 @@ public void MockFileSystem_DefaultState_CurrentDirectoryExists(string currentDir Assert.IsTrue(actualCurrentDirectory.Exists); } + + [Test] + public void MockFileSystem_FileSystemWatcher_ShouldBeAssignable() + { + var path = XFS.Path(@"C:\root"); + var fileSystem = new MockFileSystem {FileSystemWatcher = new TestFileSystemWatcherFactory()}; + var watcher = fileSystem.FileSystemWatcher.FromPath(path); + Assert.AreEqual(path, watcher.Path); + } + + private class TestFileSystemWatcherFactory : IFileSystemWatcherFactory + { + public FileSystemWatcherBase CreateNew() => new TestFileSystemWatcher(null); + public FileSystemWatcherBase FromPath(string path) => new TestFileSystemWatcher(path); + } + + private class TestFileSystemWatcher : FileSystemWatcherBase + { + public TestFileSystemWatcher(string path) => Path = path; + public override string Path { get; set; } + public override bool IncludeSubdirectories { get; set; } + public override bool EnableRaisingEvents { get; set; } + public override string Filter { get; set; } + public override int InternalBufferSize { get; set; } + public override NotifyFilters NotifyFilter { get; set; } +#if NET40 + public override ISite Site { get; set; } + public override ISynchronizeInvoke SynchronizingObject { get; set; } + public override void BeginInit() {} + public override void EndInit() {} +#endif + public override WaitForChangedResult WaitForChanged(WatcherChangeTypes changeType) => default(WaitForChangedResult); + public override WaitForChangedResult WaitForChanged(WatcherChangeTypes changeType, int timeout) => default(WaitForChangedResult); + } } } diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemWatcherFactoryTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemWatcherFactoryTests.cs index 53cc83ec5..49696508a 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemWatcherFactoryTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemWatcherFactoryTests.cs @@ -1,8 +1,5 @@ using NUnit.Framework; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using XFS = System.IO.Abstractions.TestingHelpers.MockUnixSupport; namespace System.IO.Abstractions.TestingHelpers.Tests { @@ -10,43 +7,18 @@ namespace System.IO.Abstractions.TestingHelpers.Tests public class MockFileSystemWatcherFactoryTests { [Test] - public void MockFileSystemWatcherFactory_CreateNew_ShouldReturnNonNullMockWatcher() + public void MockFileSystemWatcherFactory_CreateNew_ShouldThrowNotImplementedException() { - // Arrange var factory = new MockFileSystemWatcherFactory(); - - // Act - var result = factory.CreateNew(); - - // Assert - Assert.IsNotNull(result); + Assert.Throws(() => factory.CreateNew()); } [Test] - public void MockFileSystemWatcherFactory_FromPath_ShouldReturnNonNullMockWatcher() + public void MockFileSystemWatcherFactory_FromPath_ShouldThrowNotImplementedException() { - // Arrange + var path = XFS.Path(@"y:\test"); var factory = new MockFileSystemWatcherFactory(); - - // Act - var result = factory.FromPath(@"y:\test"); - - // Assert - Assert.IsNotNull(result); - } - - [Test] - public void MockFileSystemWatcherFactory_FromPath_ShouldReturnWatcherForSpecifiedPath() - { - // Arrange - const string path = @"z:\test"; - var factory = new MockFileSystemWatcherFactory(); - - // Act - var result = factory.FromPath(path); - - // Assert - Assert.AreEqual(path, result.Path); + Assert.Throws(() => factory.FromPath(path)); } } } diff --git a/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs b/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs index ca540cd51..f66d2f7f8 100644 --- a/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs +++ b/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs @@ -65,7 +65,7 @@ public MockFileSystem(IDictionary files, string currentDir public IDriveInfoFactory DriveInfo { get; } - public IFileSystemWatcherFactory FileSystemWatcher { get; } + public IFileSystemWatcherFactory FileSystemWatcher { get; set; } public IFileSystem FileSystem => this; diff --git a/System.IO.Abstractions.TestingHelpers/MockFileSystemWatcher.cs b/System.IO.Abstractions.TestingHelpers/MockFileSystemWatcher.cs deleted file mode 100644 index a1204412d..000000000 --- a/System.IO.Abstractions.TestingHelpers/MockFileSystemWatcher.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System.ComponentModel; - -namespace System.IO.Abstractions.TestingHelpers -{ - public class MockFileSystemWatcher : FileSystemWatcherBase - { - public override bool IncludeSubdirectories { get; set; } - public override bool EnableRaisingEvents { get; set; } - public override string Filter { get; set; } - public override int InternalBufferSize { get; set; } - public override NotifyFilters NotifyFilter { get; set; } - public override string Path { get; set; } -#if NET40 - public override ISite Site { get; set; } - public override ISynchronizeInvoke SynchronizingObject { get; set; } -#endif - -#if NET40 - public override void BeginInit() - { - } - - public override void EndInit() - { - } -#endif - - public override WaitForChangedResult WaitForChanged(WatcherChangeTypes changeType) - { - throw new NotImplementedException(); - } - - public override WaitForChangedResult WaitForChanged(WatcherChangeTypes changeType, int timeout) - { - throw new NotImplementedException(); - } - } -} diff --git a/System.IO.Abstractions.TestingHelpers/MockFileSystemWatcherFactory.cs b/System.IO.Abstractions.TestingHelpers/MockFileSystemWatcherFactory.cs index 14a494074..fa832ba13 100644 --- a/System.IO.Abstractions.TestingHelpers/MockFileSystemWatcherFactory.cs +++ b/System.IO.Abstractions.TestingHelpers/MockFileSystemWatcherFactory.cs @@ -1,21 +1,12 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace System.IO.Abstractions.TestingHelpers +namespace System.IO.Abstractions.TestingHelpers { [Serializable] public class MockFileSystemWatcherFactory : IFileSystemWatcherFactory { - public FileSystemWatcherBase CreateNew() - { - return new MockFileSystemWatcher(); - } + public FileSystemWatcherBase CreateNew() => + throw new NotImplementedException(StringResources.Manager.GetString("FILE_SYSTEM_WATCHER_NOT_IMPLEMENTED_EXCEPTION")); - public FileSystemWatcherBase FromPath(string path) - { - return new MockFileSystemWatcher {Path = path}; - } + public FileSystemWatcherBase FromPath(string path) => + throw new NotImplementedException(StringResources.Manager.GetString("FILE_SYSTEM_WATCHER_NOT_IMPLEMENTED_EXCEPTION")); } } diff --git a/System.IO.Abstractions.TestingHelpers/Properties/Resources.resx b/System.IO.Abstractions.TestingHelpers/Properties/Resources.resx index 5e8750581..13097ff13 100644 --- a/System.IO.Abstractions.TestingHelpers/Properties/Resources.resx +++ b/System.IO.Abstractions.TestingHelpers/Properties/Resources.resx @@ -144,4 +144,7 @@ Could not find file '{0}'. - \ No newline at end of file + + MockFileSystem does not have a built-in FileSystemWatcher implementation. You must provide your own mock or implementation of IFileSystemWatcherFactory and assign it to MockFileSystem.FileSystemWatcher. + +