Verify the number of times a protected method is called using Moq

后端 未结 2 1331
你的背包
你的背包 2021-01-01 21:47

In my unit-tests I\'m mocking a protected method using Moq, and would like to assert that it is called a certain number of times. This question describes something similar f

相关标签:
2条回答
  • 2021-01-01 22:07

    In the Moq.Protected namespace, there is an IProtectedMock interface that has a Verify method taking Times as a parameter.

    Edit This is available since at least Moq 4.0.10827. Syntax example:

    testBaseMock.Protected().Setup("ChildMethod1");
    
    ...
    testBaseMock.Protected().Verify("ChildMethod1", Times.Once());
    
    0 讨论(0)
  • 2021-01-01 22:07

    To augment Ogata's answer, we can also verify a protected method that takes arguments:

    testBaseMock.Protected().Setup(
        "ChildMethod1",
        ItExpr.IsAny<string>(),
        ItExpr.IsAny<string>());
    
    testBaseMock.Protected().Verify(
        "ChildMethod1", 
        Times.Once(),
        ItExpr.IsAny<string>()
        ItExpr.IsAny<string>());
    

    For instance, that would verify ChildMethod1(string x, string y).

    See also: http://www.nudoq.org/#!/Packages/Moq.Testeroids/Moq/IProtectedMock(TMock)/M/Verify

    0 讨论(0)
提交回复
热议问题