最小化派生类的基类函数 [英] Moq a base class function from a derived class

查看:75
本文介绍了最小化派生类的基类函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Moq的新手,我刚刚看了关于Moqing的多元视觉视频,所以我感到有能力去做一些测试.我有一个基类,假设Sheet实现了一个ISheet接口. Sheet是页面的基类:

I am new to Moq and I just watched the pluralsight video on Moqing so I felt empowered to go and write some tests. I have a Base Class let’s say Sheet which implements an interface ISheet. Sheet is the base class for pages:

abstract class Sheet: ISheet
{
    public virtual void CreateSheet()  // Defined in ISheet
    {
    }
    public virtual void BuildSheet() // Defined in ISheet
    {
    }
    //and some abstract methods, etc.
}

public class Page : Sheet
{
    public override void CreateSheet()
    {
        BuildSheet(); // Base class implementation
    }
}

我覆盖了基类中的一种方法CreateSheet(),但是我想测试基类中的BuildSheet()方法是从我的派生类中调用的:

I overrode one of the methods from the base class which is CreateSheet(), but I want to test that the BuildSheet() method in the base class is called from my derived class:

因此,在我的测试课程中,我要对SUT而不是接口进行量测

So in my Test Class, I Moq the SUT instead of the interface

var MockSheet = new Moq<Page>();

调用方法:

var actual = MockSheet.Object.CreateSheet(); 

然后验证

MockSheet.Verify(x => x.BuildSheet(), Times.AtLeastOnce);

相反,我至少收到了一次MockException对模拟的预期调用,但从未执行过. CreateSheet方法永远不会被调用.如果我将他的CreateSheet方法更改为:

Instead I get MockException "Expected Invocation on the mock at least once but was never performed. The CreateSheet method never gets called. If I change he CreateSheet method to:

public void CreateDocSheet() // removed override
{
    BuildSheet() // base classses implementation
}

在测试通话中:

var actual = MockSheet.Object.CreateDocSheet();  

有效.我不明白为什么.我认为从功能上讲,这是一种有效的编码方式,我有一张纸,有两页,我有一个定义所有应实现的功能和属性的接口,我在基类中实现了这些功能,但不是全部方法需要在派生类中实现,因此,某些方法将被覆盖,而某些方法将使用基类实现.请解释为什么使用该功能的覆盖版本会引起问题?

It works. I don’t understand why. I believe that functionally that this is a valid way of coding, I have sheet, that has 2 pages, I have an Interface that defines all the functions and properties that should be implemented, I implement these functions in the base class, but not all methods need to implemented in the derived classes so, some method are overridden and some use the base class implementation. Please explain why it is an issue to use the overridden version of the function?

推荐答案

如果您更改

var MockSheet = new Moq<Page>();

进入

var MockSheet = new Moq<Page> { CallBase = true, };

您的模拟程序(您可以将其视为Page的派生类)将在模拟程序自身对CreateSheet的替代中从Page调用实现.

your mock (which you can think of as a derived class of Page) will call the implementation from Page in the mock's own override of CreateSheet.

默认行为(如果不更改CallBase)是Moq覆盖它可以使用的所有方法和属性,并使用 empty 实现.如我所说,将CallBase设置为true会使Moq调用Page实现.

The default behavior (if you do not change CallBase) is for Moq to override every method and property it can, and use an empty implementation. Setting CallBase to true makes Moq call the Page implementation instead, like I said.

(当然,如果您希望CreateDocSheet做一些不重要的事情,请使用MockSheet.Setup(x => x.CreateDocSheet()).Callback(() => { /* something */ }).)

(Of course, use MockSheet.Setup(x => x.CreateDocSheet()).Callback(() => { /* something */ }) if you want CreateDocSheet to do something non-trivial.)

如果您从CreateSheet中删除了virtual修饰符,则Moq不能再覆盖或嘲笑该成员.想一想:Moq如何模拟"一种非虚拟方法?因此,通话完全绕过了Moq的课程.

In the case where you removed the virtual modifier from CreateSheet, Moq can no longer override or mock this member. Think of it: How could Moq "mock" a non-virtual method? Therefore, the call bypasses Moq's class completely.

这篇关于最小化派生类的基类函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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