简体   繁体   English

验证是否使用Moq 3.1调用了基本受保护的方法

[英]Verify that a base protected method is called with Moq 3.1

I have a class that inherits from an abstract base class. 我有一个从抽象基类继承的类。 I am trying to verify that a specified protected method in the base class is called twice and I'd like to verify that the parameters passed are specific values (different for each call). 我试图验证在基类中指定的受保护方法是否被调用了两次,并且我想验证传递的参数是否为特定值(每个调用都不同)。

I was hoping that I'd be able to use Protected with either Expect or Verify , but seemingly I've missed what can be done with these methods. 我希望可以使用ExpectVerify Protected ,但是似乎我错过了使用这些方法可以完成的工作。

Is what I'm attempting possible with moq? 我想尝试的起订量可能吗?

UPDATE: An example of what I'm trying to do: 更新:我正在尝试做的一个例子:

class MyBase
{
    protected void SomeMethodThatsAPainForUnitTesting(string var1, string var2)
    {
        //Stuf with file systems etc that's very hard to unit test
    }
}

class ClassIWantToTest : MyBase
{
    public void IWantToTestThisMethod()
    {
        var var1 = //some logic to build var 1
        var var2 = //some logic to build var 2
        SomeMethodThatsAPainForUnitTesting(var1, var);
    }
}

Essentially I want to test the way the variables var1 and var2 are created correctly and passed into SomeMethodThatsAPainForUnitTesting , so essentially I want to mock out the protected method, verify that it was called at least once and that the parameters were all passed correctly. 本质上,我想测试正确创建变量var1和var2并将其传递给SomeMethodThatsAPainForUnitTesting ,因此本质上我想模拟出受保护的方法,验证它至少被调用一次并且参数都正确传递。 If this was calling a method on an interface it would be trivial, but I'm coming unstuck with a protected method. 如果这是在接口上调用方法,那将是微不足道的,但是我对受保护的方法感到困惑。

I can't easily change the design as it's brown field development and I'm not the only class calling the method. 我不能轻易更改设计,因为它是开发领域,不是唯一调用该方法的类。

No it's isn't possible. 不,这是不可能的。

Tools like Moq and Rhino mocks work their magic by generating subclasses of the types you want to mock at run time. Moq和Rhino模拟之类的工具通过在运行时生成要模拟的类型的子类来发挥其魔力。 They generate all their "Verify" and "Expect" logic by overriding a member (in the case of virtual members) or implementing it (in the case of an interface) and inserting code to inspect or record the passed arguments and return the pre-canned response. 他们通过重写成员(对于虚拟成员)或实现它(对于接口),并插入代码以检查或记录传递的参数并返回pre-,来生成所有的“验证”和“期望”逻辑。罐头回应。

So what can you do to get around this? 那么,您该怎么做才能解决这个问题? Firstly if you could alter the base method to be virtual this would then allow you to test your method by creating a test harness like so:- 首先,如果您可以将基本方法更改为虚拟方法,则可以通过创建如下这样的测试工具来测试方法:

class ClassIWantToTestHarness : ClassIWantToTest {
    public string Arg1 { get; set; }
    public string Arg2 { get; set; }
    public int CallCount { get; set; }

    protected override void SomeMethodThatsAPainForUnitTesting(var1, var2) {
        Arg1 = var1;
        Arg2 = var2;
            CallCount++;
    }
}

[Test]
public void ClassIWantToTest_DoesWhatItsSupposedToDo() {
    var harness = new ClassIWantToTestHarness();
    harness.IWantToTestThisMethod();
    Assert.AreEqual("Whatever", harness.Arg1);
    Assert.AreEqual("It should be", harness.Arg2);
    Assert.IsGreaterThan(0, harness.CallCount);
}

If it's not possible to alter the base class at all due to the code being so horribly brownfield you don't want to get your shoes dirty, you could simply wrap the method in ClassIWantToTest as a protected virtual method and perform the same trick on that instead. 如果由于代码太可怕而根本无法更改基类,而又不想弄脏鞋子,则可以将方法包装在ClassIWantToTest中作为受保护的虚拟方法,并在该方法上执行相同的技巧代替。 Moq does have support for overriding protected virtual members (see the miscellaneous section here ) - though personally I prefer manual test harnesses in this case. Moq确实支持覆盖受保护的虚拟成员(请参阅此处的其他部分)-尽管在这种情况下,我个人更喜欢手动测试工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM