diff --git a/TestHelpers.Tests/MockFileCopyTests.cs b/TestHelpers.Tests/MockFileCopyTests.cs index 7dffee39f..1a38846d9 100644 --- a/TestHelpers.Tests/MockFileCopyTests.cs +++ b/TestHelpers.Tests/MockFileCopyTests.cs @@ -271,5 +271,29 @@ public void MockFile_Copy_ShouldThrowArgumentExceptionWhenTargetIsEmpty_Message( Assert.That(exception.Message, Is.StringStarting("Empty file name is not legal.")); } + + [Test] + public void MockFile_Copy_ShouldThrowFileNotFoundExceptionWhenSourceFileDoesNotExist_Message() + { + var sourceFileName = XFS.Path(@"c:\source\demo.txt"); + var destFileName = XFS.Path(@"c:\destination\demo.txt"); + var fileSystem = new MockFileSystem(); + // The "real" System.IO.File throws a FileNotFoundException before checking if that the destination folder exists, therefore there is no need to create it. + + var exception = Assert.Throws(() => fileSystem.File.Copy(sourceFileName, destFileName)); + Assert.That(exception.Message, Is.EqualTo(XFS.Path(@"Could not find file 'c:\source\demo.txt'."))); + } + + [Test] + public void MockFile_Copy_ShouldThrowFileNotFoundExceptionWhenSourceFileDoesNotExist_FileName() + { + var sourceFileName = XFS.Path(@"c:\source\demo.txt"); + var destFileName = XFS.Path(@"c:\destination\demo.txt"); + var fileSystem = new MockFileSystem(); + // The "real" System.IO.File throws a FileNotFoundException before checking if that the destination folder exists, therefore there is no need to create it. + + var exception = Assert.Throws(() => fileSystem.File.Copy(sourceFileName, destFileName)); + Assert.That(exception.FileName, Is.EqualTo(sourceFileName)); + } } } \ No newline at end of file diff --git a/TestHelpers.Tests/MockFileInfoTests.cs b/TestHelpers.Tests/MockFileInfoTests.cs index 659820d56..9c0f186c1 100644 --- a/TestHelpers.Tests/MockFileInfoTests.cs +++ b/TestHelpers.Tests/MockFileInfoTests.cs @@ -476,5 +476,39 @@ public void MockFileInfo_MoveTo_ShouldUpdateFileInfoDirectoryAndFullName() Assert.AreEqual(fileInfo.DirectoryName, destinationFolder); Assert.AreEqual(fileInfo.FullName, destination); } + + [Test] + public void MockFileInfo_CopyTo_ShouldThrowFileNotFoundExceptionWhenSourceFileDoesNotExist_Message() + { + //Arrange + var sourceFileName = XFS.Path(@"c:\source\demo.txt"); + var destFileName = XFS.Path(@"c:\destination\demo.txt"); + + var fileSystem = new MockFileSystem(); + var fileInfo = fileSystem.FileInfo.FromFileName(sourceFileName); + // The "real" FileInfo throws a FileNotFoundException before checking if that the destination folder exists, therefore there is no need to create it. + + //Act + var exception = Assert.Throws(() => fileInfo.CopyTo(destFileName)); + + Assert.That(exception.Message, Is.EqualTo(XFS.Path(@"Could not find file 'c:\source\demo.txt'."))); + } + + [Test] + public void MockFileInfo_CopyTo_ShouldThrowFileNotFoundExceptionWhenSourceFileDoesNotExist_FileName() + { + //Arrange + var sourceFileName = XFS.Path(@"c:\source\demo.txt"); + var destFileName = XFS.Path(@"c:\destination\demo.txt"); + + var fileSystem = new MockFileSystem(); + var fileInfo = fileSystem.FileInfo.FromFileName(sourceFileName); + // The "real" FileInfo throws a FileNotFoundException before checking if that the destination folder exists, therefore there is no need to create it. + + //Act + var exception = Assert.Throws(() => fileInfo.CopyTo(destFileName)); + + Assert.That(exception.FileName, Is.EqualTo(sourceFileName)); + } } } \ No newline at end of file diff --git a/TestingHelpers/MockFile.cs b/TestingHelpers/MockFile.cs index b83489935..6d8c48beb 100644 --- a/TestingHelpers/MockFile.cs +++ b/TestingHelpers/MockFile.cs @@ -112,14 +112,20 @@ public override void Copy(string sourceFileName, string destFileName, bool overw mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(sourceFileName, "sourceFileName"); mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(destFileName, "destFileName"); + var sourceFileExists = mockFileDataAccessor.FileExists(sourceFileName); + if (!sourceFileExists) + { + throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, Properties.Resources.COULD_NOT_FIND_FILE_EXCEPTION, sourceFileName), sourceFileName); + } + var directoryNameOfDestination = mockPath.GetDirectoryName(destFileName); if (!mockFileDataAccessor.Directory.Exists(directoryNameOfDestination)) { throw new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, Properties.Resources.COULD_NOT_FIND_PART_OF_PATH_EXCEPTION, destFileName)); } - var fileExists = mockFileDataAccessor.FileExists(destFileName); - if (fileExists) + var destFileExists = mockFileDataAccessor.FileExists(destFileName); + if (destFileExists) { if (!overwrite) { diff --git a/TestingHelpers/Properties/Resources.Designer.cs b/TestingHelpers/Properties/Resources.Designer.cs index ece6e4eaf..1efa20d96 100644 --- a/TestingHelpers/Properties/Resources.Designer.cs +++ b/TestingHelpers/Properties/Resources.Designer.cs @@ -69,6 +69,15 @@ internal static string ACCESS_TO_THE_PATH_IS_DENIED { } } + /// + /// Looks up a localized string similar to Could not find file '{0}'.. + /// + internal static string COULD_NOT_FIND_FILE_EXCEPTION { + get { + return ResourceManager.GetString("COULD_NOT_FIND_FILE_EXCEPTION", resourceCulture); + } + } + /// /// Looks up a localized string similar to Could not find a part of the path '{0}'.. /// diff --git a/TestingHelpers/Properties/Resources.resx b/TestingHelpers/Properties/Resources.resx index d47ca2bdc..6d1bb51a2 100644 --- a/TestingHelpers/Properties/Resources.resx +++ b/TestingHelpers/Properties/Resources.resx @@ -141,4 +141,7 @@ File name cannot be null. + + Could not find file '{0}'. + \ No newline at end of file