单元测试方法:使用文件IO [英] Unit Testing Methods With File IO

查看:233
本文介绍了单元测试方法:使用文件IO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图进入的编写单元测试的习惯,我已经写了前几个,但他们平时一直很基本的...我想开始做一招TDD因为我想改善的我的代码(设计和结构)的质量 - 减少耦合,而在同一时间有希望减少其通过向可测试构建滑回归数目

I'm trying to get into the habit of writing unit tests, I've written a few before but they've usually been quite basic...I'd like to start making a move to TDD as i want to improve the quality of my code (design and structure) - reducing coupling, while at the same time hopefully reduce number of regressions which slip through to a testable build.

我有采取了相对简单的项目对我的工作开始。 ,得到的程序监视的文件夹,然后作用于该文件夹中的文件

I have taken a relatively simple project i work on to begin with. The resultant program watches a folder and then acts on files within this folder.

下面是从项目中提取一些代码,一个典型的例子:

Here is a typical example of some code extracted from the project:

private string RestoreExtension(String file)
    {
        var unknownFile = Path.GetFileName(file);
        var ignoreDir = Path.GetDirectoryName(file) + "\\Unknown";
        string newFile;

        //We need this library for determining mime
        if (CheckLibrary("urlmon.dll"))
        {
            AddLogLine("Attempting to restore file extension");
            var mime = GetMimeType(BufferFile(file, 256));
            var extension = FileExtensions.FindExtension(mime);
            if (!String.IsNullOrEmpty(extension))
            {
                AddLogLine("Found extension: " + extension);
                newFile = file + "." + extension;
                File.Move(file, newFile);
                return newFile;
            }
        }
        else
        {
            AddLogLine("Unable to load urlmon.dll");
        }

        AddLogLine("Unable to restore file extension");

        if (!Directory.Exists(ignoreDir))
        {
            Directory.CreateDirectory(ignoreDir);
        }
        var time = DateTime.Now;
        const string format = "dd-MM-yyyy HH-mm-ss";

        newFile = ignoreDir + "\\" + unknownFile + "-" + time.ToString(format);
        File.Move(file, newFile);

        return String.Empty;
    }



问题:

Questions :

如何在使用IO我测试的方法?
我真的不希望使用一个真正的文件,因为这将是缓慢的(多集成测试的?),但我不能真正看到另一种方式。我可以添加一个可切换IO抽象层,但是这听起来好像它可能不必要地复杂化的代码...

How can i test a method using IO? I don't really want to be using a real file as this would be slow (and more of an integration test?), but i cant really see another way. I could add a switchable IO abstraction layer, but this sounds to me like it may unnecessarily complicate code...

是这样的价值单元测试的项目?
我的意思是,是不是太简单了。一旦你去掉.Net和第三方库调用有所剩无几......那么,这一个情况下的工作量,使其可测试意味着它不是一个很好的候选人,以测试?

Is a project like this worth unit testing? By that i mean, is it too simple. Once you strip out .Net and 3rd party library calls there is not much left...So is it a case that the amount of work to make it testable means its not a good candidate to test?

很多在这个项目中的方法是私有的,因为这个项目恰好是一个Windows服务。我读过你应该只真正测试这是外部可见的(公共或通过一个接口暴露)的方法,这是不可能在这种情况下,但是,我要揭露任何我自己的方法。那么,这个项目值得测试(如ID可能需要或者改变方法访问修饰符公共或添加某些属性,使NUnit的可以看到他们)?

A lot of the methods in this project are private, as this project happens to be a windows service. I've read you should only really test methods which are externally visible (public or exposed through an interface), it is unlikely in this case however that i want to expose any of my own methods. So is this project worth testing (as id probably need to either change method access modifiers to public or add some attributes so that NUnit can see them)?

干杯

推荐答案

至于如何测试文件I / O:
在包装这种操作的封装文件I / O的一种常见方式类。例如:

Regarding how to test File I/O: A common way of doing this is encapsulating File I/O in a wrapper class. For example:

public class FileHandler : IFileHandler
{
     public string GetFilename(string name)
     {
         return System.IO.GetFileName(name);
     }

    public string GetDirectoryName(string directory)
    {
         return System.IO.GetDirectoryName(directory);
    }
}

public interface IFileHandler
{
      string GetFilename(string name);
      string GetDirectoryName(string directory);
}

在你的方法测试,你只对接口程序。当你运行你的单元测试,你可以使用一个模拟对象返回预定义值。为此,你可以使用起订量

In your method under test, you only program against the interface. When you run your unit test, you would use a mock object which returns predefined values. For this, you could use Moq.

这是一些工作要做,但那么你就需要测试文件I / O以及

This is a bit work to do but then you do not need to test File I/O as well.

最佳
一月

这篇关于单元测试方法:使用文件IO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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