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
Hi,

I am new to TypeMock and am testing the product. Please answer my question from the Reflective mocks viewpoint.

Below, I have duplicated the code from the TypeMock API Reference for MockedInstance. I have a test scenario similar to this, but in the CreateAnObject() method instead of the TestClass being created with "new", it is created with a static factory method of a 3rd party library. For example, replace
TestClass theClass = new TestClass();
with:
TestClass theClass = ThirdPartyLibClass.CreateTestClass();
How do I get a mockedInstance of TestClass in this case ? When I try this, "theClass" comes back null. I don't have access to the source code of ThirdPartyLibClass. I just know it returns a TestClass object.
Thanks

--- Code below duplicated from TypeMock API Reference

public static void CreateAnObject()
{
TestClass theClass = new TestClass();
theClass.field = 5;
...
}
[Test]
public void VerifyFutureObject()
{
//Create new Mock for a future Object, no expectations
Mock mock = MockManager.Mock(typeof(TestClass));

// Call our code
TheClass.CreateAnObject();

// get the mocked object and test the field
TestClass theClass = mock.MockedInstance as TestClass;
Assert.AreEqual(5, theClass.field);

// Verify that all the expected calls have actually been called.
MockManager.Verify();
}
asked by tomindenver (1.6k points)

2 Answers

0 votes
Hi, and welcome aboard

usualy when you want a real instance with mocking ability the most simple way is to use the MockObject class.
MockObject mock = MockManager.MockObject(typeof(TestClass));
TestClass instance = (TestClass)mock.Object;


Depending on what you need to acomplish, you can bypass the creation of the object and simply create it using the MockObject API.
Or if you cant/dont want to bypass the object creation phase, try using the MockManager.MockAll API. (MockAll will mock all instances out of a given class and should cause the factory to return a mocked instance.)

If this doesnt works as well you can always try to mock out the Factory itself.
answered by lior (13.2k points)
0 votes
Hi,
We have actually created the Tracer exactly for this scenario.
You will be able to see exactly what instances are created (although you will only see one type in the Community Edition)
Here is a way to mock this method:
// create a mocked TestClass Object 
MockObject<TestClass> testMock = MockManager.MockObject<TestClass>();
// make factory return our mocked instance
Mock factoryMock = MockManager.Mock<ThirdPartyLibClass>();
factoryMock.ExpectAndReturn("CreateTestClass",testMock.Object);
answered by scott (32k points)
...