Verifying that a mocked method have been called with Moq

public void It_Should_Call_The_Number_Generator_Service()
    var result = _numbersProvider.GetNumbersCollection(10);
    //We are checking that the GetNumber()
    //method has been called exactely 10 times
    _numberGeneratorServiceMock.Verify(m => m.GetNumber(It.IsAny<int>(), It.IsAny<int>()),
    //We can also check that the mocked method
    //has been called with the desired parameters
    _numberGeneratorServiceMock.Verify(m => m.GetNumber(It.Is<int>(min => min == 1), It.Is<int>(max => max == 10)),

Is possible to reset the calls on a mock object by calling the ResetCalls() method. In this way is possible to test it again against a different condition.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s