Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have following method in my class and I want to write unit test for it using nunit and rhino mocks. My method moves all the folders from source path to destination path, I am new to NUnit and Rhinomocks and I dont know how to mock System.IO.Directory or System.IO.DirectoryInfo objects. I have tried everything on google.

C#
public void MoveDirectories()
{
    var dirList = (from f in new DirectoryInfo(sourceDir).EnumerateDirectories("*")
                   select f).ToList();

    destinationDir = destinationDir + "//" + newDir;

    foreach (var d in dirList)
    {
        if (GetWeekOfYear(d.CreationTime) == weekNo)
        {
            if (!Directory.Exists(destinationDir))
            {
                Directory.CreateDirectory(destinationDir);
            }

            if (!Directory.Exists(destinationDir + "//" + d.Name))
            {
                Console.WriteLine("Move Folder  " + d.FullName + " TO " + destinationDir + "//" + d.Name);
                Directory.Move(d.FullName, destinationDir + "//" + d.Name);
            }
        }
    }
}


Here GetWeekOfYear returns the Week number for a current date or date supplied.
If any one can help me this.
Posted
Updated 4-Aug-13 22:48pm
v2

1 solution

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);
C#
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
 
Share this answer
 
v2
Comments
mitesh_a 5-Aug-13 8:51am    
Hi Thanks for your answer, if you don't mind can you please also give me example for how to mock the IDirectoryManager sorry for asking more as I am new to Mocking I don't understand how to proceed further and how to test my MoveDirectories() method in unit test.

Here I am sending you my code for the reference.

Interfaces
==========

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Maintenance.Interfaces
{
public interface ISystemMaintenance
{
int GetWeekOfYear(DateTime currentDate);

void MoveDirectories();

IEnumerable<system.io.directoryinfo> GetDirectories(string sourcePath);
}
}


Concreet Implementaion
======================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Globalization;
using System.IO;

using Maintenance.Interfaces;

namespace ClassLibrary
{
public class SystemMaintenance : ISystemMaintenance
{
private string sourceDir;
private string destinationDir;
private int weekNo;
private string newDir;

private IEnumerable<system.io.directoryinfo> dirList;

public SystemMaintenance(string sourceDir, string destinationDir)
{
this.sourceDir = sourceDir;
this.destinationDir = destinationDir;
this.weekNo = GetWeekOfYear(DateTime.Now);
this.newDir = DateTime.Now.Year.ToString() + "-" + weekNo.ToString();

this.dirList = GetDirectories(sourceDir);
}

public int GetWeekOfYear(DateTime currentDate)
{
int weekNo;

DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(currentDate);

if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
{
currentDate = currentDate.AddDays(3);
}

weekNo = CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(currentDate, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

return weekNo;
}

public void MoveDirectories()
{

destinationDir = destinationDir + "//" + newDir;

foreach (var d in dirList)
{
if (GetWeekOfYear(d.CreationTime) == weekNo)
{
if (!Directory.Exists(destinationDir))
{
Directory.CreateDirectory(destinationDir);
}

if (!Directory.Exists(destinationDir + "//" + d.Name))
{
Console.WriteLine("Move Folder " + d.FullName + " TO " + destinationDir + "//" + d.Name);
Directory.Move(d.FullName, destinationDir + "//" + d.Name);
}
}
}
}

public IEnumerable<system.io.directoryinfo> GetDirectories(string sourcePath)
{
var dirList = (from f in new DirectoryInfo(sourcePath).EnumerateDirectories("*")
select f).ToList();
return dirList;
}
}
}


Main Program
============
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Globalization;
using System.IO;

using ClassLibrary;
using Maintenance.Interfaces;

namespace ConsoleApplication
{
class CleanSystem
{
static string sourceDir = @"C:\Maintenance\Success\TestDir";
public static string destinationDir = @"C:\Maintenance\History";


static void Main(string[] args)
{

ISystemMaintenance sysMaintain;

sysMaintain = new SystemMaintenance(sourceDir, destinationDir);

sysMaintain.MoveDirectories();

Console.ReadLine();
}
}
}

Thanks,

Mitesh.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900