Adam
Adam

Reputation: 3013

Mocking COM Interfaces using Rhino Mocks

I have a COM library that I have to reference in my app and I am trying to mock its interfaces.

I am getting exceptions when I am doing this MockRepository.GenerateMock<IAmAComInterface>();

I don't get exceptions when I do this: MockRepository.GenerateDynamicMockWithRemoting<IAmAComInterface>(); but none of my expectations are verifying.

Am I doing something wrong?

For now, I have a bunch of wrappers for all of my COM interfaces and I am mocking them, but I would really like to not have to wrap everything.

EDIT:
Exceptions with GenerateMock:
System.TypeLoadException

With the message of:
The type is marked as eligible for type equivalence, but either it has generic parameters, or it is not a structure, COM imported interface, enumeration, or delegate.

When using GenerateDynamicMockWithRemoting test failure always says Expected: 1 Actual: 0 for any expectations on the COM interface.

Using Rhino.Mocks 3.6.

Upvotes: 5

Views: 1957

Answers (2)

Manushin Igor
Manushin Igor

Reputation: 3689

I solved the same problem by this solution (from question How to test a COM dependent object in C#): https://stackoverflow.com/a/4333388/185498

Try to set "Embed Interop Types" to FALSE for assembly that contains COM interface.

Upvotes: 3

PatrickSteele
PatrickSteele

Reputation: 14677

Looks like this is an issue with .NET 4.0's "Type Equivalence". See this for more details: http://code.google.com/p/moq/issues/detail?id=254

The fix (as noted above) is easy by adding:

Castle.DynamicProxy.Generators.AttributesToAvoidReplicating.Add(typeof (TypeIdentifierAttribute));

To your unit test.

Upvotes: 13

Related Questions