Mock a private Method of class with a private Constructor

4 posts, 0 answers
  1. Robert
    Robert avatar
    20 posts
    Member since:
    Jul 2013

    Posted 05 Nov 2013 Link to this post

    I have the following class, I use this class to assign functionality to another class.  How do I mock the "PrivatelyDoWork" method using JustMock?  I also want to Assert that the "DoPrivateWork" has been call for a certain number of occurances.  Thanks.
    internal class MyClass
    {
        private MyClass()
        {
        }
     
        private void PrivatelyDoWork(string stringValue)
        {
        }
     
        public static Action<object> HowMyClassWorks()
        {
            return (myString) => new MyClass().PrivatelyDoWork((string)myString);
        }
    }
  2. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 06 Nov 2013 Link to this post

    Hi Robert,

    Thank you for bringing this question.

    You can mock the PrivatelyDoWork() method as any other non-public member. You can try something like this:
    Mock.NonPublic.Arrange(myMock, "PrivatelyDoWork", ArgExpr.IsAny<string>()).DoNothing().Occurs(1);
    The above will arrange the PrivatelyDoWork method as follows: Whenever it is called with any string as an argument, it should do nothing. In the same time the method should occur exactly once during the test execution.

    For this to work, I used InternalsVisibleTo atribute inside the AssemblyInfo of MyClass' assembly:
    [assembly: InternalsVisibleTo("MyTestProject")]
    The above allows me to create a mock instance of MyClass: Mock.Create<MyClass>();

    Please, check this article of our online help documentation. In it you will find a lot of interesting examples about mocking non-public classes and members.

    I hope this helps. Let me know if I can be of a further assistance.

    Regards,
    Kaloyan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  3. DevCraft R3 2016 release webinar banner
  4. Robert
    Robert avatar
    20 posts
    Member since:
    Jul 2013

    Posted 06 Nov 2013 Link to this post

    I don't understand how you got past the private constructor.  You have "myMock" in your arrange method, but I don't know where it comes from.  The example that you pointed me to has a public constructor.
  5. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 06 Nov 2013 Link to this post

    Hi again Robert,

    I apologize for missing this in my reply.

    Let's assume we have the following class (it is almost the same as yours):
    internal class MyClass
    {
        private MyClass()
        {
        }
     
        private void PrivatelyDoWork(string stringValue)
        {
            throw new NotImplementedException();
        }
     
        public void CallPrivateFunc()
        {
            this.PrivatelyDoWork("test");
        }
    }

    After adding the InternalsVisibleTo attribute, you will be able to write the following test:
    [TestMethod]
    public void ShouldMockPrivateConstructorAndPrivateMethodInsideInternalClass()
    {
        // Arrange
        var myMock = Mock.Create<MyClass>(Constructor.Mocked, Behavior.CallOriginal);
     
        Mock.NonPublic.Arrange(myMock, "PrivatelyDoWork", ArgExpr.IsAny<string>())
            .DoNothing().Occurs(1);
     
        // Act
        myMock.CallPrivateFunc();
     
        // Assert
        Mock.Assert(myMock);
    }
    The above test follows this logic:
    1. We create a mocked instance of MyClass. Also, we are mocking the constructor and applying Behavior.CallOriginal so we can later act on it.
    2. We arrange the PrivatelyDoWork method: Whenever it is called with any string as an argument, it should do nothing. In the same time the method should occur exactly once during the test execution.
    3. We act on the system under test by calling the public CallPrivateFunc function. It will execute the arranged PrivatelyDoWork method.
    4. We assert on the whole mock.

    I hope this helps.

    Regards,
    Kaloyan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Back to Top