单身到facilate在传统code基本单元测试。一个好主意或没有?好主意、单元测试、单身、传统

2023-09-04 23:49:49 作者:一鹿伴晗

伙计们,     我有一大笔遗产的.Net code群,我想介绍单元测试的团队。他们是好人,但是这是所有新的给他们(说实话这是相当新的给我也行)。

Folks, I've a large legacy .Net code base, and I'm trying to introduce Unit Testing to the team. They're good guys but this is all new to them (to be honest it's reasonably new to me too).

问题之一是code碱基大量使用静态类System.IO,有广泛的内部库静态类和类都没有写入的接口(除非有一个实际的设计原因这样做的)。

One of the problems is the code base makes heavy use of static classes in System.IO, there are extensive in-house libraries of static classes and classes aren't written to interfaces (unless there's an actual design reason to do so).

我开发一个易于它的命名策略使用NUnit和FakeItEasy。

I'm developing an ease-it-in strategy using NUnit and FakeItEasy.

要解决静态类依赖性我已经写了生成的包装类和接口为现有的静态类的工具。例如在一个配置文件,我说我要封装的 System.IO目录和放大器;文件,该工具生成与code线沿线的一个组件。 。

To address the static class dependencies I've written a tool that generates wrapper classes and interfaces for existing Static Classes. e.g. In a config file I say I want wrappers for System.IO Directory & File, the tool generates an assembly with code along the lines of . . .

public interface IFile
{
    // All Method signatures taken from System.IO.File
}

internal class File
    : IFile
{
    // All Methods delegate to System.IO.File
}

public interface IIO
{
    IFile File {get;}
    IDirectory Directory {get;}
}

internal class IO
    : IIO
{
    public IFile File {get; private set;}
    public IDirectory Directory {get; private set;}
    public IO()
    {
        File = new File();
        Directory = new Directory();
    }
}


public static class IO
{
    public IIO Instance {get; set;}
    static IO()
    {
        Instance = new IO();
    }
}

我们的想法是,在现有的code基可以通过发现简单地更新/更换和添加​​新的项目引用,并在单元测试中,你可以指定一个模仿对象,以 IO .Instance

一,一方面我很高兴使用这种方法,它提供了一个干净,快速的方法来直接使用静态类离开,再加上为我们提供了一种方法来模拟这些静态类,让我们试code表示取决于他们。

One one hand I'm quite happy with this approach, it gives a clean and fast way to move away from using static classes directly, plus gives us a way to mock those static classes and lets us test code that depends on them.

在另一方面,我有种感觉我已经做了处理与魔鬼,我把它换成隐单身的依赖与明确单身的依赖关系。

On the other hand, I kind of feel I've done a deal with the devil, I've replaced dependencies on implicit singletons with dependencies on explicit singletons.

这一切会去可怕的错误,我的某个时候在不久的将来,或者你认为这是一个可行的办法?

Is this all going to go horribly wrong for me sometime in the near future, or do you think this is a workable approach?

推荐答案

像你一样的遗留应用程序,我会怎么做。没有什么可以做,而不必重构大块。它仍然是灵活的,因为你可以通过静态类变更实施。

I would do as you did for a legacy application. Not much you can do without having to refactor larger pieces. It's still flexible since you can change implementation through the static class.

一个想法可能是只让实例方法,静态的,在你的国际奥委会注册的IO类,而不是(作为一个单身)。然后让国际奥委会找到正确的实施(这是由静态实例方法返回)。

One idea could be to only make the Instance method static and register the IO class in your IOC instead (as a singleton). Then let the IOC find the correct implementation (which is returned by the static Instance method).

尼斯介绍的IoC容器:的http://joelabrahamsson.com/entry/inversion-of-control-introduction-with-examples-in-dotnet

Nice introduction to IoC containers: http://joelabrahamsson.com/entry/inversion-of-control-introduction-with-examples-in-dotnet