chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
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 by donniedarko (3.8k 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);
    }
}

 

answered by alex (17k points)
...