如何使用起订量在抽象类测试的具体方法? [英] How to use moq to test a concrete method in an abstract class?

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

问题描述

在过去,当我想嘲笑一个抽象类,我只是在代码中创建一个嘲笑类,扩展抽象类,然后使用这个类在我的单元测试...

In the past when I wanted to mock an abstract class I'd simply create a mocked class in code that extended the abstract class, then used that class in my unit testing...

public abstract class MyConverter : IValueConverter
{
    public abstract Object Convert(...) { ... };

    public virtual Object ConvertBack(...) { ... }
}

private sealed class MockedConverter : MyConverter { ... }

[TestMethod]
public void TestMethod1()
{
    var mock = new MockedConverter();

    var expected = ...;
    var actual = mock.ConvertBack(...);

    Assert.AreEqual(expected, actual);
}



我要养成使用起订量,而不是习惯。我不知道我怎么会去有关使用起订量来测试我的抽象类的默认返回值。这里有什么建议?

I want to get into the habit of using Moq instead. I'm not sure how I'd go about using Moq to test the default return value of my abstract class. Any advice here?

推荐答案

如果您设置 CallBase 来真的, ,将调用基类的实现。

If you set CallBase to true, it will invoke the base class implementation.

var mock = new Mock<MyConverter> { CallBase = true };



见的自定义快速启动部分仿制品的行为。

See the Customizing Mock Behavior section of the Quick Start.

调用基类的实现,如果没有期望覆盖成员(在犀牛制品又名部分嘲弄):默认为false

Invoke base class implementation if no expectation overrides the member (a.k.a. "Partial Mocks" in Rhino Mocks): default is false.

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

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