2

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.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Gregory Prescott
  • 574
  • 3
  • 10
  • 2
    If you need that, you're [doing it wrong](http://stackoverflow.com/questions/105007/should-i-test-private-methods-or-only-public-ones). Reading a file and processing it are entirely different things. Split up that logic and test it separately. – CodeCaster Feb 18 '16 at 09:56
  • Thank you for advice, but it's my learning task. Can't change the logic, but i need to test it as i was told to do – Gregory Prescott Feb 21 '16 at 08:08

1 Answers1

3

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.

Eva
  • 449
  • 2
  • 8