You wrap the calls to static methods on
DirectoryInfo
in a class of your own that implements an interface.
Something like this;
public interface IDirectoryManager
{
bool Exists(string path);
DirectoryInfo Create(string path);
void Move(String sourceDirName, String destDirName);
IEnumerable<DirectoryInfo> EnumerateDirectories(string path, string searchPattern);
}
public class DirectoryManager : IDirectoryManager
{
public bool Exists(string path)
{
return Directory.Exists(path);
}
public DirectoryInfo Create(string path)
{
return Directory.CreateDirectory(path);
}
public void Move(string sourceDirName, string destDirName) {
Directory.Move(sourceDirName, destDirName);
}
public IEnumerable<DirectoryInfo> EnumerateDirectories(string path, string searchPattern) {
var dir = new DirectoryInfo(path);
return dir.EnumerateDirectories(searchPattern);
}
}
Then, in you class that implements your
MoveDirectories
method, you have that class take an
IDirectoryManager
instance in its constructor and then only use that in the
MoveDirectory
method.
Something like this (obviously with all you current members and parameters also there);
class YourClass {
private readonly IDirectoryManager manager;
public YourClass(IDirectoryManager manager) {
this.manager = manager;
}
public void MoveDirectories() {
var dirList = manager.EnumerateDirectories(sourceDir, "*");
string destinationDir + "//" + newDir;
foreach (var d in dirList) {
if (GetWeekOfYear(d.CreationTime) == weekNo) {
if (!manager.Exists(destinationDir)) {
manager.Create(destinationDir);
}
if (!manager.Exists(destinationDir + "//" + d.Name)) {
Console.WriteLine("Move Folder " + d.FullName + " TO " + destinationDir + "//" + d.Name);
manager.Move(d.FullName, destinationDir + "//" + d.Name);
}
}
}
}
}
Now you can mock the
IDirectoryManager
in your test case.
Hope this helps,
Fredrik