如何使用抽象方法测试抽象类中的方法?

如何使用抽象方法测试抽象类中的方法?

问题描述:

有必要检查抽象'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.