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

好久不见. 提交于 2019-11-30 11:35:21
Jeff Ogata

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!