简体   繁体   中英

How to verify the number of private method calls in typemock

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM