如何测试一个抽象类,抽象方法的方法? [英] How to test a method in an abstract class with abstract methods?

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

问题描述

这是要检查执行的MyMethod虚方法在抽象的MyAbstractClass:

It is necessary to check implementation of 'MyMethod' virtual method in the abstract 'MyAbstractClass':

public abstract MyAbstractClass
{
    public void MyMethod()
    {
        Testpassed = true;
    }

    public abstract int StatusCode{get;internal set;} // **EDIT**: internal setter was added

    public bool TestPassed{get;private set;}
}

我试着做到以下几点:

I've tried to do the following:

[TestMethod()]
{
    Mock<MyAbstractClass> mockClass = new Mock<MyAbstractClass>();
    mockClass.Object.MyMethod();
    Assert.IsTrue(mockClass.Object.TestPassed);
}

在试图执行的MyMethod会生成以下错误:

on attempt to execute 'MyMethod' the following error is generated:

法类型'set_Status code'   MyAbstractClass`2Proxy0434d60c0f4542fb9b4667ead4ca3a18   从程序集DynamicProxyGenAssembly2,   版本= 0.0.0.0,文化=中性公钥=空'不具有   实施..

Method 'set_StatusCode' in type 'MyAbstractClass`2Proxy0434d60c0f4542fb9b4667ead4ca3a18' from assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation..

请指教,我怎么能得到解决这个问题?

Please advise, how could I get the problem resolved?

非常感谢!

P.S。其实,我可以继承MyAbstractClass并提供实现'状态code'的属性,但我必须要实现的一堆属性...

P.S. Actually, I could inherit from MyAbstractClass and provide implementation for 'StatusCode' property, but I have a bunch of properties to be implemented...

推荐答案

请参阅<一href="http://stackoverflow.com/questions/1980108/moq-verifying-a-method-was-called/2000357#2000357">this回答有关部分的嘲讽,我认为你必须提供一个具体的实现。

See this answer about partial mocking and I think you'll have to provide a concrete implementation.

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

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