验证是否使用Moq 3.1调用了基本受保护的方法 [英] Verify that a base protected method is called with Moq 3.1

查看:88
本文介绍了验证是否使用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).

我希望可以将ProtectedExpectVerify一起使用,但似乎我错过了使用这些方法可以完成的工作.

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.

我想用最小起订量来实现吗?

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

基本上,我想测试变量var1和var2正确创建并传递到SomeMethodThatsAPainForUnitTesting的方式,因此本质上,我想模拟出受保护的方法,验证它至少被调用一次并且参数全部正确通过.如果这是在接口上调用方法,那将是微不足道的,但是我对受保护的方法感到困惑.

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. 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.

推荐答案

不可能.

Moq和Rhino模拟之类的工具通过在运行时生成要模拟的类型的子类来发挥其魔力.他们通过重写成员(对于虚拟成员)或实现它(对于接口),并插入代码以检查或记录传递的参数并返回pre-,来生成所有的验证"和期望"逻辑.罐头回应.

Tools like Moq and Rhino mocks work their magic by generating subclasses of the types you want to mock at run time. 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.

那么您可以怎么做才能解决这个问题?首先,如果您可以将基本方法更改为虚拟方法,则可以通过创建如下这样的测试工具来测试方法:-

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

如果由于代码太可怕而根本无法更改基类,而又不想弄脏鞋子,则可以将方法包装在ClassIWantToTest中,作为受保护的虚拟方法,然后执行相同的操作取而代之. Moq确实支持覆盖受保护的虚拟成员(请参见此处 )-尽管我个人更喜欢在这种情况下使用手动测试工具.

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. 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 3.1调用了基本受保护的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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