Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions TestHelpers.Tests/MockFileCopyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FileNotFoundException>(() => 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<FileNotFoundException>(() => fileSystem.File.Copy(sourceFileName, destFileName));
Assert.That(exception.FileName, Is.EqualTo(sourceFileName));
}
}
}
34 changes: 34 additions & 0 deletions TestHelpers.Tests/MockFileInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FileNotFoundException>(() => 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<FileNotFoundException>(() => fileInfo.CopyTo(destFileName));

Assert.That(exception.FileName, Is.EqualTo(sourceFileName));
}
}
}
10 changes: 8 additions & 2 deletions TestingHelpers/MockFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
9 changes: 9 additions & 0 deletions TestingHelpers/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions TestingHelpers/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,7 @@
<data name="FILENAME_CANNOT_BE_NULL" xml:space="preserve">
<value>File name cannot be null.</value>
</data>
<data name="COULD_NOT_FIND_FILE_EXCEPTION" xml:space="preserve">
<value>Could not find file '{0}'.</value>
</data>
</root>