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

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

问题描述

有必要检查抽象'MyAbstractClass'中'MyMethod'虚方法的实现:

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;}
}

我尝试执行以下操作:

[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_StatusCode"'我的抽象类`2Proxy0434d60c0f4542fb9b4667ead4ca3a18'来自程序集 'DynamicProxyGenAssembly2,Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' 没有实施..

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?

非常感谢!

附言实际上,我可以从 MyAbstractClass 继承并提供StatusCode"属性的实现,但是我有一堆要实现的属性...

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

推荐答案

参见 this answer 关于部分模拟,我认为您必须提供具体的实现.

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

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

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