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

发布于 2024-09-16 18:28:04 字数 908 浏览 4 评论 0原文

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

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”时,会生成以下错误:

类型中的方法“set_StatusCode” 'MyAbstractClass`2Proxy0434d60c0f4542fb9b4667ead4ca3a18' 来自程序集“DynamicProxyGenAssembly2”, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' 没有 实施..

请指教,我怎样才能解决问题?

多谢!

PS 实际上,我可以继承 MyAbstractClass 并提供“StatusCode”属性的实现,但我有一堆属性要实现......

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

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

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?

Thanks a lot!

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

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

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

发布评论

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

评论(3

百变从容 2024-09-23 18:28:04

您发布的代码运行良好(除了轻微的拼写错误)。我根本无法让它因同样的错误而失败。 Moq 在运行时实现抽象属性并使其返回默认值(在本例中为 0)。尝试更新版本的 Moq。

我还警告不要将测试逻辑放入类中。如果 TestPassed 的唯一目的是用于测试,那么它绝对不属于那里。如果您发布真实的代码,我们可以提供更多帮助。

The code that you posted runs fine (except for minor misspellings). I simply could not get it to fail with the same error. Moq implements the abstract property at runtime and makes it return the default value(0 in this case). Try a more recent version of Moq.

I would also caution against putting test logic into the class. If the only purpose of TestPassed is for testing than it definitely doesn't belong there. We could help a great deal more if you post real code.

擦肩而过的背影 2024-09-23 18:28:04

请参阅这个答案关于部分模拟,我想你会必须提供具体的实施方案。

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

飘过的浮云 2024-09-23 18:28:04

我知道这不是您最小起订量问题的答案,但无论如何。即使您有很多抽象属性,VS 也可以非常轻松地添加空(实际上是抛出)实现。因此,如果 MyMethod 在设计上不依赖于这些属性,我将添加一个空的派生类并对其进行测试。

I understand that it's not an answer to your moq question, but anyway. Even if you have a lot of abstract properties, VS makes it extremely easy to add empty (actually, throwing) implementations. So, if MyMethod does not depend on these properties by design, I'd add an empty derived class and test it.

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