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
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
I am trying to mock a call to a private method but seem to be unable to do so.

This is an example of what I'm trying to test.

public class MyClassToTest
{

    public bool TestInternalMethod(int valueToTest)
    {
        bool result = IsNumberEven(valueToTest);
        return result;
    }

    private bool IsNumberEven(int valueToTest)
    {
        bool result = (valueToTest % 2) == 0;
        return result;
    }

}



And I've tried both of the test below.

/// <summary>
///A test for TestInternalMethod
///</summary>
[TestMethod()]
public void TestInternalMethodTest1()
{
    MyClassToTest target = new MyClassToTest();
    MyClassToTest_Accessor accessor = new MyClassToTest_Accessor();
    MockManager.Init();
    MockManager.ClearAll();
    using (RecordExpectations recorder = RecorderManager.StartRecording())
    {
        accessor.IsNumberEven(7);
        recorder.Return(true);
    }
    bool actual = target.TestInternalMethod(7);
    Assert.AreEqual(true, actual);

    MockManager.Verify();
}

/// <summary>
///A test for TestInternalMethod
///</summary>
[TestMethod()]
public void TestInternalMethodTest2()
{
    MyClassToTest target = new MyClassToTest();
    MockManager.Init();
    MockManager.ClearAll();

    Mock mockObjectTest = MockManager.Mock(typeof(MyClassToTest));
    mockObjectTest.ExpectAndReturn("IsNumberEven", true);

    bool actual = target.TestInternalMethod(7);
    Assert.AreEqual(true, actual);

    MockManager.Verify();
}



In both cases, the private method is not being mocked and the result is coming out as false even though I coded to return true for the call to IsNumberEven. Can anyone see what I'm doing wrong?

Oh, yes. I'm using VS 2008, .Net 3.5 in WIndows Vista 64 bit.

TIA

John
asked by John (640 points)

2 Answers

0 votes
Hi,

Its just a matter of ordering.
The "Magic" of Typemock Isolator is happening when new object is created. when you create a Mock it will be attached to the next created object of that type. In your case the real objects was created before the Mock.

Look at the following code:

[TestMethod()] 
[VerifyMocks]
public void TestInternalMethodTest2() 
{ 
    Mock mockObjectTest = MockManager.Mock(typeof(MyClassToTest)); 
    mockObjectTest.ExpectAndReturn("IsNumberEven", true); 

    MyClassToTest target = new MyClassToTest(); 
    bool actual = target.TestInternalMethod(7); 
    Assert.AreEqual(true, actual); 
}


:!: A word of cautious, many view the need to to mock private methods as an indication that a design change may be needed.
answered by lior (13.2k points)
0 votes
Thank you Lior,

I thought it would be something simple!

The reason I want to mock the private method is because the method deals with things that I can't test the result of such as printing documents.

John
answered by John (640 points)
...