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
Is it possible to mock private methods on a test class?
asked by jflowers (1.5k points)

11 Answers

0 votes
Hi
Of course it is possible! 8)
In reflective mocks it is straight forward just like
you will do with public methods.

    class TheClass
    {
        public int Method()
        {
            return PrivateMethod();
        }

        private int PrivateMethod()
        {
            return 1;
        }
    }

    [TestFixture]
    public class TestClass
    {
        [Test]
        public void Test()
        {
            Mock mock = MockManager.Mock(typeof(TheClass));
            mock.ExpectAndReturn("PrivateMethod", 5);

            TheClass c = new TheClass();
            Assert.AreEqual(5, c.Method());
        }
    }


You can mock private methods using natural mocks if you are using
Visual Studio Team System.
See the details here
answered by ohad (35.5k points)
0 votes
Hmm,
It looks like this would work with MbUnit's analog:
http://vadim-net.blogspot.com/2007/04/t ... bunit.html

Jay
answered by jflowers (1.5k points)
0 votes
Ooh I forgot to ask. So lets say I don't have VSTS for Devs and the private method I want to mock is on my TestSubject. I know that the Natural style will let you mock single calls. Is this possible with reflective mocking?
answered by jflowers (1.5k points)
0 votes
Hi
OK I understand now that you want to call directly to private methods
using TypeMock.
Currently this is not supported by TypeMock.
(As I showed you mocking private methods is possible)
I will add that to our features request list.
answered by ohad (35.5k points)
0 votes
No. I don't want to call the private method. I want to mock it, just it and nothing else on that object.

So my test is Testing TestSubject.AddProduct. TestSubject.AddProduct calls a private method TestSubject.CreateProduct. CreateOrder is a long and complex method that ultimately calls a significant portion of the system. This is motivating me to take it out of the equation for testing AddProduct.

I hope I have made things more clear.

Thanks,
Jay
answered by jflowers (1.5k points)
0 votes
Hi
If you want to mock a private method using reflective mocks
it is quiet easy.
I just use your methods names with no regards to the implementation
of course :)

The tested class code:
Public Class OrderMaster
    Private Function CreateProduct() As Integer
        CreateProduct = 1
    End Function

    Public Function AddProduct()
        AddProduct = CreateProduct()
    End Function
End Class


The test code
    <Test()> _
    Public Sub TestMethod()

        Dim mock As Mock = MockManager.Mock(GetType(OrderMaster))
        mock.ExpectAndReturn("CreateProduct", 100)

        Dim om As OrderMaster = New OrderMaster()
        Assert.AreEqual(100, om.AddProduct())

    End Sub
answered by ohad (35.5k points)
0 votes
Oh! It is not tied to an instance?

Thanks!
answered by jflowers (1.5k points)
0 votes
It seems that prevents the real constructor from being called. That is not desired here. Am I wrong? If not is there a way to have the real constructor called?
answered by jflowers (1.5k points)
0 votes
I found right after the previous post. That is controlled in the creation of the mock:

MockManager.Mock(GetType(API.Base.OrderInfo.OrderMaster), False)
answered by jflowers (1.5k points)
0 votes
Hi
You are right.
I should have mention that by default constructors are mocked
when using MockManager.Mock :oops:
By the way when using MockManager.MockObject the constructor is NOT mocked by default ...
One note:
MockManager.Mock(Type, Boolean)
is obsolete use instead
MockManager.Mock(GetType(API.Base.OrderInfo.OrderMaster), Constructor.NotMocked)
answered by ohad (35.5k points)
...