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

We are creating a dynamic mock to a COM object by mocking the interop assembly. However when we mock the constructor, the COM object is still called. We know this as we get a message from the actual COM object. Is mocking COM objects supported?

Example code:
MockManager.Init();

MockObject dynaMock = MockManager.MockObject(typeof(InteropAssembly));

InteropAssembly interop = dynaMock.Object as InteropAssembly;
TestClass x = new TestClass(interop);

MockManager.Verify();
asked by cjlotz (3k points)

3 Answers

0 votes
Hi,
TypeMock.NET can mock .NET code, it cannot mock native code.
This is the reason that Interop and P/Invoke methods cannot be mocked.

You can of course mock a wrapper class.
answered by scott (32k points)
0 votes
Scott

The interop assembly as the COM callable wrapper is the wrapper object for the COM object so I assumed that we should be able to mock this as it is .NET code.
answered by cjlotz (3k points)
0 votes
Hi,
This should work, just remember that P/Invoke methods cannot be mocked.

If you are having problems. please send a small demo of the InteropAssembly so we can help.

Just as an afterthough, we should throw an error when trying to mock P/Invoke messages. I'll see if we can do this.
answered by scott (32k points)
...