如何正确地测试一个抽象类 [英] how to properly test an abstract class

查看:276
本文介绍了如何正确地测试一个抽象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在一个抽象类,名为组件创建单元测试的过程。 VS2008编译我的程序没有问题,所以我能够创建解决方案中的一个单元测试项目。有一件事我注意到,虽然是测试文件被创建时,有这些方法,我从来没有见过的:

I'm currently in the process of creating a unit test for an abstract class, called Component. VS2008 compiled my program no problems so I was able to create a unit test project within the solution. One thing I've noticed, though, is that when the test file has been created, there are these methods which I've never seen before:

internal virtual Component CreateComponent()
        {
            // TODO: Instantiate an appropriate concrete class.
            Component target = null;
            return target;
        }


internal virtual Component_Accessor CreateComponent_Accessor()
        {
            // TODO: Instantiate an appropriate concrete class.
            Component_Accessor target = null;
            return target;
        }



我相信这是创建一个具体的组件类。

在每个试验方法,有这样一行:

Within each Test method, there is this line:

Component目标= CreateComponent (); // TODO:初始化为适当的值

我怎么初始化这个适当的值?或者说,我怎么实例化一个合适的具体类,如上述的 CreateComponent CreateComponent_Accessor 方法?

how do I initialize this to an appropriate value? Or, how do I instantiate an appropriate concrete class, as stated above by the CreateComponent and the CreateComponent_Accessor methods?

下面是抽象类的构造函数,附加信息:

here is the constructor of the abstract class, for additional info:

保护元件( eVtCompId inComponentId,eLayer inLayerId,IF_SystemMessageHandler inMessageHandler)

推荐答案

您不能实例化一个抽象类。所以,你可以写一个模拟实现这个抽象类(你应该实现抽象成员)在你的单元测试的项目,然后打电话给你试图测试方法。你可以有不同的模拟实现,以测试你的类的各种方法。

You cannot instantiate an abstract class. So you could write a mock implementation of this abstract class (where you should implement the abstract members) in your unit test project and then call the methods you are trying to test. You could have different mock implementations in order to test various methods of your class.

作为替代编写模拟实现,你可以使用一个模拟的框架,如犀牛制品,起订量,NSubstitute,......这能简化这一任务,并允许您定义类的抽象成员的预期。

As an alternative to writing a mock implementation you could use a mock framework such as Rhino Mocks, Moq, NSubstitute, ... which could simplify this task and allow you to define expectations for the abstract members of the class.

更新:

由于在评论部分要求在这里是一个例子。

As requested in the comments section here's an example.

让我们假设你有下面要进行单元测试抽象类:

Let's suppose that you have the following abstract class that you want to unit test:

public abstract class FooBar
{
    public abstract string Foo { get; }

    public string GetTheFoo()
    {
        return "Here's the foo " + Foo;
    }
}

现在在你的单元测试项目中,您可以通过实现它写一个派生类实现抽象成员嘲笑值:

Now in your unit test project you could implement it by writing a derived class implementing the abstract members with mocked values:

public class FooBarMock : FooBar
{
    public override string Foo 
    { 
        get { return "bar" } 
    }
}

然后你可以写对 GetTheFoo 方法单元测试:

and then you could write your unit test against the GetTheFoo method:

// arrange
var sut = new FooBarMock();

// act
var actual = sut.GetTheFoo();

// assert
Assert.AreEqual("Here's the foo bar", actual);

和使用模拟框架(MOQ在我的例子),你并不需要实现这个抽象类单元测试,但你可以直接使用模拟框架来定义所测试的方法是建立在依靠抽象成员的期望:

and with a mock framework (Moq in my example) you do not need to implement this abstract class in the unit test but you could directly use the mocking framework to define expectations of the abstract members that the method under test is relying upon:

// arrange
var sut = new Mock<FooBar>();
sut.Setup(x => x.Foo).Returns("bar");

// act
var actual = sut.Object.GetTheFoo();

// assert
Assert.AreEqual("Here's the foo bar", actual);

这篇关于如何正确地测试一个抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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