使用通用抽象类型参数进行.net单元测试链接

发布于 2024-09-30 03:16:32 字数 556 浏览 7 评论 0原文

对采用泛型类型参数(其中类型也是抽象类)的抽象类进行单元测试的正确方法是什么?例如)

public abstract class BaseClass<T> where T : BaseT { ... }

我必须创建一个扩展 BaseT 的测试类,对吧?

如果 BaseClass 支持方法链接,会发生什么情况,

public abstract class BaseClass<T> where T : BaseClass<T> { ... } ??

谢谢。

更新了第二种情况的具体类示例:

    public class ConcreteClass : BaseClass<ConcreteClass>
    {
        public ConcreteClass Method1()
        {
            return this as ConcreteClass;
        }
    }

What would be the proper way to unit test an abstract class which takes generic type parameter where the type is also an abstract class? E.g)

public abstract class BaseClass<T> where T : BaseT { ... }

I'll have to create a test class which extends BaseT, right??

and what happen if the BaseClass support method chaining such that

public abstract class BaseClass<T> where T : BaseClass<T> { ... } ??

thanks.

updated concrete class sample for the 2nd case:

    public class ConcreteClass : BaseClass<ConcreteClass>
    {
        public ConcreteClass Method1()
        {
            return this as ConcreteClass;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

莫相离 2024-10-07 03:16:32

您可以在不使用 Rhino Mocks 实现虚拟实现类的情况下执行此操作: http:// /www.ayende.com/wiki/Rhino+Mocks+Partial+Mocks.ashx

不幸的是,Rhino Mocks 要求您使用带有部分模拟的记录/播放语法,而不是 AAA 语法。

给定以下抽象类:

public abstract class BaseAbstract
{
    public abstract int Increment();
}

public abstract class AbstractClass<T> where T : BaseAbstract
{
    private int inc;

    public abstract T Abstract();

    public virtual int Concrete()
    {
        return inc += Abstract().Increment();
    }
}

您可以通过以下方式测试 Concrete 的实现:

[Test]
public void mock_an_abstract_class_with_generic()
{
    var mocks = new MockRepository();
    var baseAbstract = mocks.StrictMock<BaseAbstract>();
    var abstractClass = mocks.PartialMock<AbstractClass<BaseAbstract>>();

    using (mocks.Record())
    {
        baseAbstract.Stub(a => a.Increment()).Return(5);
        abstractClass.Stub(a => a.Abstract()).Return(baseAbstract);
    }

    using (mocks.Playback())
    {
        abstractClass.Concrete().ShouldEqual(5);
        abstractClass.Concrete().ShouldEqual(10);
    }
}

本质上,您为基类创建部分模拟,设置对抽象方法的期望,然后调用被测试的具体方法。泛型只是另一个模拟,如果需要的话,另一个部分。

这样做的缺点显然是对记录/回放语法的要求。我不知道其他模拟框架是否可以为您提供更多帮助,但我通常发现,如果您有这样的高级用例,Rhino Mocks 是您可以使用的模拟框架。

You can do this without implementing a dummy implementation class using Rhino Mocks: http://www.ayende.com/wiki/Rhino+Mocks+Partial+Mocks.ashx

Unfortunately, Rhino Mocks requires you to use the record/playback syntax with partial mocks instead of the AAA syntax.

Given the following abstract classes:

public abstract class BaseAbstract
{
    public abstract int Increment();
}

public abstract class AbstractClass<T> where T : BaseAbstract
{
    private int inc;

    public abstract T Abstract();

    public virtual int Concrete()
    {
        return inc += Abstract().Increment();
    }
}

You can test the implementation of Concrete in the following way:

[Test]
public void mock_an_abstract_class_with_generic()
{
    var mocks = new MockRepository();
    var baseAbstract = mocks.StrictMock<BaseAbstract>();
    var abstractClass = mocks.PartialMock<AbstractClass<BaseAbstract>>();

    using (mocks.Record())
    {
        baseAbstract.Stub(a => a.Increment()).Return(5);
        abstractClass.Stub(a => a.Abstract()).Return(baseAbstract);
    }

    using (mocks.Playback())
    {
        abstractClass.Concrete().ShouldEqual(5);
        abstractClass.Concrete().ShouldEqual(10);
    }
}

In essence, you create a partial mock for the base class, set expectations on the abstract methods, then call the concrete method under test. The generic is simply another mock, another partial if necessary.

The downside of this is obviously the requirement for the record/playback syntax. I don't know if other mocking frameworks can help you out more here, but I've generally found that Rhino Mocks is the mocking framework you go to if you have an advanced use case like this.

命比纸薄 2024-10-07 03:16:32

在不了解更多细节的情况下,我猜测您正在尝试测试在抽象泛型类中实现的一些代码。例如,您可以测试在基类中实现且未标记为抽象的公共方法或属性。

因此,您必须创建一个扩展通用基类 BaseClass< 的类。 T>
以及扩展 BaseT 的不同类。我们将这些称为 ConcreteClass< T>和 ConcreteT 分别。

然后,实例化一个 ConcreteClass< 类型的类。具体T>。您现在可以在此实例上运行测试,从而测试抽象类的逻辑。

Without knowing more details, I'm guessing that you are trying to test some code that is implemented in the abstract generic class. For example, you might test a public method or property that is implemented in the base class and isn't marked as abstract.

So, you will have to create a class that extends the generic base class BaseClass< T >
and a different class that extends BaseT. Let's call these ConcreteClass< T > and ConcreteT respectively.

Then, instantiate a class of type ConcreteClass< ConcreteT >. You can now run tests on this instance, allowing you to test the abstract class' logic.

罗罗贝儿 2024-10-07 03:16:32

没有办法测试抽象类,你不能创建它的实例:)

There is no way to test an abstract class, you cannot create an instance of :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文