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.
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:
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.
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.
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.
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 void PrivatelyDoWork(string stringValue)
throw new NotImplementedException();
public void CallPrivateFunc()
After adding the InternalsVisibleTo attribute, you will be able to write the following test:
public void ShouldMockPrivateConstructorAndPrivateMethodInsideInternalClass()
var myMock = Mock.Create<MyClass>(Constructor.Mocked, Behavior.CallOriginal);