Reputation: 13
I wrote a test case for checking file path. In that test case I used Expected exception, then I want to know what happen if the method would not throw the file not found exception.
For example, I run the test case in another system if the given file path exists in the system, the test will go to fail. But it should not be, the test case should be pass always.
How to handle that situation because the Unit test is not dependent anything example should not depend on machine?
Test case...
string sourceFilePath = @"C:\RipWatcher\Bitmap.USF_C.usf";
string targetDirectory = @"C:\UploadedRipFile\";
[Test]
[ExpectedException(typeof(FileNotFoundException))]
public void InvalidSourceFilePathThrowsFileNotFoundException()
{
logWriter= new Mock<LogWriter>().Object;
ripfileUploader = new RipFileUploader(logWriter);
ripfileUploader.Upload(@"D:\RipWatcher\Bitmap.USF_C.usf",
targetDirectory);
}
Method..
public void Upload(string sourceFilePath, string targetFilePath)
{
if (!File.Exists(sourceFilePath))
{
throw new FileNotFoundException(string.Format("Cannot find the file: {0}", sourceFilePath));
}
if (!Directory.Exists(targetFilePath))
{
throw new DirectoryNotFoundException(string.Format("Cannot find the Directory: {0}", targetFilePath));
}
try
{
fileCopyEx.CopyEx(sourceFilePath,targetFilePath);
}
catch (Exception ex)
{
throw new Exception(string.Format("Failed to move file {0}", sourceFilePath), ex);
}
}
Upvotes: 0
Views: 2913
Reputation: 26846
If you want such a method to be testable and independent from machine it runs on - you should not use File
and Directory
classes directly.
Instead extract interface having methods from all classes you need, write an implementation of this interface which uses methods of File
and Directory
classes.
public interface IFileManager
{
bool IsFileExists(string fileName);
....
}
public class FileManager : IFileManager
{
public bool IsFileExists(string fileName)
{
return File.Exists(fileName);
}
}
public void Upload(string sourceFilePath, string targetFilePath, IFileManager fileManager)
{
if (!fileManager.IsFileExists(sourceFilePath))
{
....
}
}
In working environnment you will use this implementation, and in testing environment you have to create mocking object implementing this interface. So you can setup this mock in any way you need.
[Test]
[ExpectedException(typeof(FileNotFoundException))]
public void InvalidSourceFilePathThrowsFileNotFoundException()
{
fileManager = new Mock<IFileManager>();
fileManager.Setup(f => f.IsFileExists("someFileName")).Returns(false);
ripfileUploader = new RipFileUploader(logWriter);
ripfileUploader.Upload(@"D:\RipWatcher\Bitmap.USF_C.usf",
targetDirectory,
fileManager.Object);
}
Upvotes: 2
Reputation: 5720
If you want deterministic Results, you have to make sure that the file does not exist on any machine.
string sourceFilePath = @"C:\RipWatcher\Bitmap.USF_C.usf";
string targetDirectory = @"C:\UploadedRipFile\";
[Test]
[ExpectedException(typeof(FileNotFoundException))]
public void InvalidSourceFilePathThrowsFileNotFoundException()
{
File.Delete(@"D:\RipWatcher\Bitmap.USF_C.usf");
logWriter= new Mock<LogWriter>().Object;
ripfileUploader = new RipFileUploader(logWriter);
ripfileUploader.Upload(@"D:\RipWatcher\Bitmap.USF_C.usf",
targetDirectory);
}
Otherwise your test is not deterministic and you can just throw it away. Another possibility would be to put the Code that accesses the file system in a separate class and give your RipFileUploader a mock implementation for the test that always throws the exception.
Upvotes: 0