Getting mock object from an instance

Hi All,

In our upcoming 4.3 version we’re adding the possibility to get a mock object directly from the instance it has been assigned for. Previously this has been possible through use of MockManager.GetMocks<>() and iterating through the results, or by retrieving the latest mock in a recording block using RecorderManager.GetLastRecorderMock().

So what is this good for? If you’re mocking multiple objects in a Natural Mocks recording block and want to quickly identify the mock object mapped to one of them, you can use RecorderManager.GetMockOf() to do the job. The mock object you retrieved can then be used to mock private methods etc.

Lets see a quick example: here we’re using GetMockOf() to get the mock object mapped to an instance and use that mock object to mock a private method call, affecting test results:

[TestMethod]
public void CountNotZero_InventoryNotEmpty()
{
using (RecordExpectations rec = RecorderManager.StartRecording())
{
Warehouse mockedInstance = new Warehouse();

// we want to mock CountInventory(). In order to do this we retrieve the mock object mapped to the mocked instance
Mock<warehouse> mock = RecorderManager.GetMockOf<Warehouse>(mockedInstance);
mock.ExpectAndReturn(“CountInventory”, 10);
}

Warehouse target = new Warehouse();

// the mocked CountInventory() will be called so InventoryEmpty should be false
Assert.IsFalse(target.InventoryEmpty);
}

This new API came from a user request, and is the result of ongoing conversations we are having with our users community in the forums and through our support mailbox. So – what API would make Typemock work better for you?

TOP