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

后端 未结 2 1343
你的背包
你的背包 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条回答
  •  萌比男神i
    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(),
        ItExpr.IsAny());
    
    testBaseMock.Protected().Verify(
        "ChildMethod1", 
        Times.Once(),
        ItExpr.IsAny()
        ItExpr.IsAny());
    

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

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

提交回复
热议问题