【问题标题】:Unit Testing protected methods using Mocks and sub classes in C#在 C# 中使用 Mocks 和子类对受保护的方法进行单元测试
【发布时间】:2019-05-07 13:53:25
【问题描述】:

我正在尝试对公共类中的一些受保护方法进行单元测试。 我从这个类继承并通过从它返回超类的方法来测试继承的类。 我的示例代码如下:

基类:

public class A
{
    // Private variables here
    public A(){ }

    protected virtual bool MethodOfA()
    {
        bool returnValue = false;
        //some operation here to set returnValue
        return returnValue;
    }

    protected bool AnotherMethodOfA()
    {
        bool anotherReturnValue = false;
        bool operationCheck = MethodOfA();

        if(operationCheck)
        {
            //do something to set the value of anotherReturnValue
        }

        return anotherReturnValue;
    }
}

继承类:

public class B : A
{
    // Private variables here
    public B():base() { }

    public new bool MethodOfA()
    {
        return base.MethodOfA();
    }

    public new bool AnotherMethodOfA()
    {
        var testMock = new Mock<A>();

        //This is the part where I'm not sure how to get it to work.
        testMock.CallBase = true; // I have tried it with and without this statement. couldn't get it to work
        testMock.Protected()
                .Setup<bool>("MethodOfA")
                .Returns(true);

        return base.AnotherMethodOfA();
    }
}

测试:

public class TestB
{
    private readonly B _sut

    //some Mocks here for setup

    public TestB()
    {
        _sut = new B();
    }


    [Fact]
    public void AnotherMethodOfA_Test()
    {
        var result = _sut.AnotherMethodOfA();
        Assert.True(result);
    }
}

我需要的基本上是,当我从 TestB 类运行我的测试时,它会在需要调用 'MethodOfA()' 的方法中点击 '_sut.AnotherMethodOfA()',它应该只使用该值我在 Mock 中提供并继续执行,而不是调用实际方法(它现在正在执行)。

现在,如果我的 Method-Under-Test 更简单并且不在其中调用另一个方法,那么整个事情就会很简单(在我的许多其他方法中都是这种情况,我已经成功地测试了这些方法),但是由于此方法在执行过程中调用了另一个方法,因此我需要模拟此中间方法并将其传递给我的被测方法。

【问题讨论】:

  • 你确定它没有因为你的设置错误而失败吗?您将其设置为返回 false .Returns(false); 而您的测试将其断言为 true?
  • 我刚刚编辑了我的问题,以免混淆,但返回不会影响断言,因为它没有经过测试。 MethodOfA 是模拟,它应该返回 true(以前为 false)。这无关紧要,因为基于该返回值,被测试的方法 (AnotherMethodOfA()) 会执行一些操作,而这应该返回 true。

标签: c# unit-testing mocking moq access-modifiers


【解决方案1】:

这很好,因为你可以用一块石头杀死两只鸟:减少或消除继承,并使用依赖注入。

与其创建protected 方法,不如想象该方法所做的事情是您的类所依赖的抽象。

代替

protected bool AnotherMethodOfA()

想象一下

public interface IThingThatDoesSomethingAndReturnsABoolean
{
    bool MethodThatReturnsBool();
}

或

public delegate bool FunctionThatReturnsBool();

然后,重写类A如下:

public class A
{
    private readonly IThingThatDoesSomethingAndReturnsABoolean _thing;

    public A(IThingThatDoesSomethingAndReturnsABoolean thing)
    {
        _thing = thing;
    }

    protected bool AnotherMethodOfA()
    {
        bool anotherReturnValue = false;
        bool operationCheck = _thing.MethodThatReturnsBool();

        if (operationCheck)
        {
            //do something to set the value of anotherReturnValue
        }

        return anotherReturnValue;
    }
}

如果您需要更改任何返回bool 的实现,则不必通过从A 继承来实现。这是一种常见的模式,但它往往会纠缠不清并产生问题,包括您所询问的确切问题。

相反,您所要做的就是提供IThingThatDoesSomethingAndReturnsABoolean 的不同实现。

现在一切都可以测试了。您可以通过提供接口模拟来测试A。您希望返回 bool 的方法是可测试的,现在是因为它不再是某个其他类的 protected 方法。

这被称为优先组合而不是继承。您无需通过相互继承来让类一起工作,而是编写单独的类来完成不同的事情并将它们组合在一起以一起工作。

如果您的依赖类需要 A 之前作为属性或字段访问的值“拥有”,您可以将它们作为方法的参数。这样可以很容易地从A 中准确了解该方法需要多少信息。

在某些情况下继承是有意义的,但是将功能分离为依赖项而不是将它们构建到继承层次结构中是一种很好的做法,它可以使您的代码可测试并避免以后可能出现的其他问题。

【讨论】:

  • 感谢您的回答。我很抱歉延迟回复,但我选择了一条完全不同的路线,因为沿着这条路走(我原来的方法,不是你建议的)会在代码库的单独部分引起其他问题。
猜你喜欢
  • 1970-01-01
  • 2010-11-10
  • 2012-02-22
  • 2017-06-24
  • 2021-05-31
  • 2015-02-26
  • 2010-09-21
  • 2012-11-05
  • 1970-01-01
相关资源
最近更新 更多