Verifying that a mocked method have been called with Moq

[Test]
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>()),
        Times.Exactly(10));
 
    //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)),
        Times.Exactly(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.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s