How to verify the number of private method calls in typemock

398 Views Asked by At

I have a public method, which calls private method, and it’s in a loop.

public void FileManipulator(StreamReader file)
{
    string line;
    while ((line = file.ReadLine()) != null)
    {
        if (checkLine(line))
        {
            //some logic
        }
        else
        {
            putToExceptions(line);
        }

    }
}

private void putToExceptions(string line)
{
    //some logic
}

How can I verify the numbers of times this inner private method was called? I’ve tried to use Isolate.Verify.GetTimesCalled, but it apparently doesn’t fit to private methods.

1

There are 1 best solutions below

0
On BEST ANSWER

Disclaimer. I work in Typemock.

You're right, Isolate.Verify.GetTimesCalled() isn't available for non-public methods. You can count the number of method was called using Isolate.WhenCalled(..).DoInstead():

[TestMethod, Isolated]
public void CountPrivateCalls()
{
    int counter = 0;
    var fileChecker = new FileChecker();
    var testFile = new StreamReader("test.txt");

    Isolate.NonPublic.WhenCalled(fileChecker, "putToExceptions").DoInstead(context =>
    {
        counter++;
        context.WillCallOriginal();
    });

    fileChecker.FileManipulator(testFile);

    Assert.AreEqual(3, counter);
}

You can read more about NonPublic API here.