Welcome to Typemock Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!

How would you verify the number of times a private method is called?

0 votes
The above is great if you just want to make sure a private method was called, but what if it is in a loop and you need to verify the number of times it was called?
asked Feb 11, 2016 by donniedarko (480 points)

1 Answer

0 votes

Hi,

Isolate.Verify.GetTimesCalled is currently not available in the NonPublic API.

This problem can be solved using DoInstead:

public class UnderTest
{
    public  void TestedMethod()
    {
        for (int i = 0; i < 5; i++)
        {
            SubjectMethod();
        }
    }
        
    private void SubjectMethod(){}
}

[TestClass, Isolated]
public class UnitTests
{
    [TestMethod]
    public void Verifying_TimesPrivateMethodWasCalled()
    {
        //Arrange
        int counter = 0;
        var underTest = new UnderTest();
        Isolate.NonPublic.WhenCalled(underTest, "SubjectMethod").DoInstead(c=>
        {
            counter++;
            c.WillCallOriginal();
        });

        //Act
        underTest.TestedMethod();

        //Assert
        Assert.AreEqual(5, counter);
    }
}

 

 

Alex Galin | Support Team

answered Feb 12, 2016 by alex (17,910 points)
...