This is a migrated thread and some comments may be shown as answers.

Mock a private Method of class with a private Constructor

3 Answers 2867 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 05 Nov 2013, 05:28 PM
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);
    }
}

3 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 06 Nov 2013, 07:52 AM
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.
0
Robert
Top achievements
Rank 1
answered on 06 Nov 2013, 05:31 PM
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.
0
Kaloyan
Telerik team
answered on 06 Nov 2013, 06:44 PM
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.
Tags
General Discussions
Asked by
Robert
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Robert
Top achievements
Rank 1
Share this question
or