使用Rhino模拟在Nunit中模拟System.IO.Directory操作 [英] Mocking System.IO.Directory operations in Nunit using Rhino mocks

查看:78
本文介绍了使用Rhino模拟在Nunit中模拟System.IO.Directory操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我的课程中有以下方法,我想使用nunit和rhino模拟为它编写单元测试。我的方法将所有文件夹从源路径移动到目标路径,我是NUnit和Rhinomocks的新手,我不知道如何模拟System.IO.Directory或System.IO.DirectoryInfo对象。我在谷歌上尝试了一切。



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.

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);
            }
        }
    }
}





这里GetWeekOfYear返回提供的当前日期或日期的周数。

如果任何人可以帮我这个。



Here GetWeekOfYear returns the Week number for a current date or date supplied.
If any one can help me this.

推荐答案

你将调用包装到静态 DirectoryInfo 中的方法,在你自己的一个实现接口的类中。



这样的东西;



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);
    }

}





然后,在你的班级实现你的 MoveDirectories 方法,你有一个类在其构造函数中获取 IDirectoryManager 实例,然后只在<$ c $中使用它c> MoveDirectory 方法。



这样的东西(显然还有当前所有成员和参数);



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);
                }
            }
        }
    }
}







现在你可以在你的测试用例中模拟 IDirectoryManager



希望这有帮助,

Fredrik




Now you can mock the IDirectoryManager in your test case.

Hope this helps,
Fredrik


这篇关于使用Rhino模拟在Nunit中模拟System.IO.Directory操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆