diff --git a/TestHelpers.Tests/MockFileCreateTests.cs b/TestHelpers.Tests/MockFileCreateTests.cs index d2dfa461b..2c3c454e3 100644 --- a/TestHelpers.Tests/MockFileCreateTests.cs +++ b/TestHelpers.Tests/MockFileCreateTests.cs @@ -18,6 +18,7 @@ public void Mockfile_Create_ShouldCreateNewStream() { string fullPath = XFS.Path(@"c:\something\demo.txt"); var fileSystem = new MockFileSystem(); + fileSystem.AddDirectory(@"c:\something"); var sut = new MockFile(fileSystem); @@ -33,6 +34,7 @@ public void Mockfile_Create_CanWriteToNewStream() { string fullPath = XFS.Path(@"c:\something\demo.txt"); var fileSystem = new MockFileSystem(); + fileSystem.AddDirectory(@"c:\something"); var data = new UTF8Encoding(false).GetBytes("Test string"); var sut = new MockFile(fileSystem); @@ -52,6 +54,7 @@ public void Mockfile_Create_OverwritesExistingFile() { string path = XFS.Path(@"c:\some\file.txt"); var fileSystem = new MockFileSystem(); + fileSystem.AddDirectory(@"c:\some"); var mockFile = new MockFile(fileSystem); @@ -146,5 +149,20 @@ public void MockFile_Create_ShouldThrowArgumentNullExceptionIfPathIsNull() var exception = Assert.Throws(action); Assert.That(exception.Message, Does.StartWith("Path cannot be null.")); } + + [Test] + public void MockFile_Create_ShouldThrowDirectoryNotFoundExceptionIfCreatingAndParentPathDoesNotExist() + { + // Arrange + var fileSystem = new MockFileSystem(); + + // Act + TestDelegate action = () => fileSystem.File.Create("C:\\Path\\NotFound.ext"); + + // Assert + Assert.IsFalse(fileSystem.Directory.Exists("C:\\path")); + var exception = Assert.Throws(action); + Assert.That(exception.Message, Does.StartWith("Could not find a part of the path")); + } } } diff --git a/TestHelpers.Tests/MockFileOpenTests.cs b/TestHelpers.Tests/MockFileOpenTests.cs index 0e8024f39..3b76c6fbf 100644 --- a/TestHelpers.Tests/MockFileOpenTests.cs +++ b/TestHelpers.Tests/MockFileOpenTests.cs @@ -42,6 +42,7 @@ public void MockFile_Open_CreatesNewFileFileOnCreate() { string filepath = XFS.Path(@"c:\something\doesnt\exist.txt"); var filesystem = new MockFileSystem(new Dictionary()); + filesystem.AddDirectory(@"c:\something\doesnt"); var stream = filesystem.File.Open(filepath, FileMode.Create); @@ -55,6 +56,7 @@ public void MockFile_Open_CreatesNewFileFileOnCreateNew() { string filepath = XFS.Path(@"c:\something\doesnt\exist.txt"); var filesystem = new MockFileSystem(new Dictionary()); + filesystem.AddDirectory(@"c:\something\doesnt"); var stream = filesystem.File.Open(filepath, FileMode.CreateNew); @@ -153,6 +155,7 @@ public void MockFile_Open_CreatesNewFileOnOpenOrCreate() { string filepath = XFS.Path(@"c:\something\doesnt\exist.txt"); var filesystem = new MockFileSystem(new Dictionary()); + filesystem.AddDirectory(@"c:\something\doesnt"); var stream = filesystem.File.Open(filepath, FileMode.OpenOrCreate); @@ -240,5 +243,20 @@ public void MockFile_OpenText_ShouldRetainCreationTime() // Assert Assert.AreEqual(creationTime, fs.FileInfo.FromFileName(filepath).CreationTime); } + + [Test] + public void MockFile_Open_ShouldThrowDirectoryNotFoundExceptionIfFileModeCreateAndParentPathDoesNotExist() + { + // Arrange + var fileSystem = new MockFileSystem(); + + // Act + TestDelegate action = () => fileSystem.File.Open("C:\\Path\\NotFound.ext", FileMode.Create); + + // Assert + Assert.IsFalse(fileSystem.Directory.Exists("C:\\path")); + var exception = Assert.Throws(action); + Assert.That(exception.Message, Does.StartWith("Could not find a part of the path")); + } } } \ No newline at end of file diff --git a/TestHelpers.Tests/MockFileTests.cs b/TestHelpers.Tests/MockFileTests.cs index f9a7c73d3..896c333fa 100644 --- a/TestHelpers.Tests/MockFileTests.cs +++ b/TestHelpers.Tests/MockFileTests.cs @@ -540,6 +540,7 @@ public void MockFile_AppendText_CreatesNewFileForAppendToNonExistingFile() { string filepath = XFS.Path(@"c:\something\doesnt\exist.txt"); var filesystem = new MockFileSystem(new Dictionary()); + filesystem.AddDirectory(@"c:\something\doesnt"); var stream = filesystem.File.AppendText(filepath); diff --git a/TestingHelpers/MockFile.cs b/TestingHelpers/MockFile.cs index ac219c693..fcb18b113 100644 --- a/TestingHelpers/MockFile.cs +++ b/TestingHelpers/MockFile.cs @@ -141,6 +141,9 @@ public override Stream Create(string path) } mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path"); + if (!mockFileDataAccessor.Directory.Exists(Path.GetDirectoryName(path))) + throw new DirectoryNotFoundException($"Could not find a part of the path '{path}'."); + mockFileDataAccessor.AddFile(path, new MockFileData(new byte[0])); var stream = OpenWrite(path); return stream;