5

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.

Adam
  • 3,014
  • 5
  • 33
  • 59
  • @Wim Coenen Edited to include exceptions and more details. – Adam Aug 10 '10 at 13:54
  • Are these COM interfaces created in .NET and designed for export to COM? Or are they COM objects with generated .NET type libraries (via tlbimp)? – PatrickSteele Aug 10 '10 at 16:56
  • @Patrick Steele They are COM objects with generated .NET type libraries via tlbimp (VS 2010 if that version makes any difference) – Adam Aug 10 '10 at 17:31

2 Answers2

13

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.

PatrickSteele
  • 14,489
  • 2
  • 51
  • 54
  • Impossible for me to call this due to this (or similar) error: Castle.DynamicProxy.Generators.AttributesToAvoidReplicating' exists in both '...Castle.Core.dll' and '...Rhino.Mocks.dll' – PandaWood Aug 09 '13 at 03:39
  • @PandaWood - See: http://stackoverflow.com/questions/8095221/how-to-resolve-castle-windsor-and-moq-version-conflicts-for-castle-core-assembly – PatrickSteele Aug 09 '13 at 13:46
  • This is brilliant! I was having issue with hosting WCF service from the mocked objects similar to explained here https://groups.google.com/forum/#!msg/rhinomocks/8PT3mLYWxVo/ZXVnBDjt-dkJ but using your example this work very well – scorpio Sep 12 '13 at 10:44
  • one issue I encountered while doing some thorough testing that if you have generated the mock for a class/interface before the statements to avoid attributes than subsequent mock generation for the same class/interface do not respect the statements and returns the mock object with attributes. So make sure that the "avoid statements" are the very first statements executed before any mock generation. – scorpio Sep 13 '13 at 10:52
3

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.

Community
  • 1
  • 1
Manushin Igor
  • 3,398
  • 1
  • 26
  • 40