Verifying a delegate was called with Moq

后端 未结 4 1256
隐瞒了意图╮
隐瞒了意图╮ 2021-02-11 14:20

i got a class that gets by argument a delegate. This class invokes that delegate, and i want to unit test it with Moq. how do i verify that this method was called ?

exam

4条回答
  •  情深已故
    2021-02-11 14:46

    As of this commit Moq now supports the mocking of delegates, for your situation you would do it like so:

    var fooMock = new Mock();
    var a = new A(fooMock.Object);
    

    Then you can verify the delegate was invoked:

    fooMock.Verify(f => f(5), Times.Once);
    

    Or:

    fooMock.Verify(f => f(It.IsAny()), Times.Once);
    

提交回复
热议问题