I am a novice in Unit and Integration testing. I have a class which contains a few methods for deleting multiple files in a specified directory and moves files to a different directory. In total there are 3 methods. Full class code:
public class FilesUtility : IFilesUtility
{
public void TidyUpXmlFiles(bool isDebug, string XmlFileDirectory)
{
if (isDebug)
{
MoveXmlFiles(XmlFileDirectory);
}
else
{
DeleteXmlFiles(XmlFileDirectory);
}
}
private static void MoveXmlFiles(string XmlFileDirectory)
{
var di = new DirectoryInfo(XmlFileDirectory);
// Create a subdirectory in the parent directory called XmlArchive.
DirectoryInfo destinationDirectory = di.CreateSubdirectory("XmlArchive");
string destinationDir = destinationDirectory.ToString();
if (!Directory.Exists(destinationDir))
{
Directory.CreateDirectory(destinationDir);
}
foreach (string file in Directory.GetFiles(XmlFileDirectory,
"*.Xml"))
{
// Use static Path method to extract only the file name from the path.
string fileName = Path.GetFileName(file);
if (fileName != null)
{
string destFile = Path.Combine(destinationDir, fileName);
//if the same file exists in the destination folder, delete the file and move the new file.
if (File.Exists(file))
{
File.Delete(destFile);
File.Move(file, destFile);
}
}
}
}
private static void DeleteXmlFiles(string XmlFileDirectory)
{
foreach (var file in Directory.GetFiles(XmlFileDirectory,
"*.Xml"))
{
File.Delete(file);
}
}
public void MoveFaultyXml(string XmlFileDirectory, string fileFullPath)
{
var di = new DirectoryInfo(XmlFileDirectory);
// Create a subdirectory in the parent directory called XmlArchive.
var destinationDirectory = di.CreateSubdirectory("FaultyXml");
var faultyXmlFileName = Path.GetFileName(fileFullPath);
var destinationDir = destinationDirectory.ToString();
if (!Directory.Exists(destinationDir))
{
Directory.CreateDirectory(destinationDir);
}
if (faultyXmlFileName != null)
{
string destFile = Path.Combine(destinationDir, faultyXmlFileName);
//if the same file exists in the destination folder, delete the file and move the new file.
if (!File.Exists(fileFullPath)) return;
File.Delete(destFile);
File.Move(fileFullPath, destFile);
}
}
I have created an Interface to this method to use Moq to Mock if that is what I need to do I am not sure. The Interface code is:
public interface IFilesUtility
{
void TidyUpXbrlFiles(bool isDebug, string xbrlFileDirectory);
void MoveFaultyXbrl(string xbrlFileDirectory, string fileFullPath);
}
Now I want to be able to test the above methods to make sure that they can actually pick up a list of XML files from a directory and delete/move all of them. I have been reading about Moq and mocking. I have also been reading about Dependency injection but my problem is I have been reading too much on it and now can't think of where to start.
I am hoping somebody can explain to me using the example code posted above how i can construct a sucessful test for this code. I have read so many questions about this on Stackoverflow and I hope you will not close this as a duplicate because like I said my problem is I have been reading too much about this. I think I might understand it if somebody could explain it to me using one of the methods in my example.
I don't know how to to do a correct [TestFixtureSetup] which I think I will need in my case where i can get a list of files to use on each test.
Thanks to all that take time to answer :)
Here's what I do to test file-handling methods:
Here's the test code...