views:

780

answers:

3

I'm trying to Mock the IUnityContainer using Moq 3.0

I'm getting a BadImageFormatException, but not when debugging. From the looks of it I'm not the only one that's ran into this problem.

here

And its a registered issue for Moq

here

I'm just curious if anyone has found a solution... closest I've found is a nice solution that uses RhinoMock by Roy Osherove

here

but I really like Moq! So I don't really want to have to switch to Rhino Mock but I will if I must

Thanks in advance!

A: 

Do you need a full-blown mock object? Could you get by with simply implementing a Fake? I.e., implementing a test instantiation of the IUnityContainer interface and overriding the method that you need to interact with?

I've fallen into the trap more than once in thinking that since I have a mock object library, I should use it for isolating every dependency in my system. More often than not, doing something simpler gets me the results I want with much lower frustration levels.

Hope this helps,

Nathan D. Southerland, MCSD.NET

Nathan Southerland
Why has been this comment down voted? Those who down voted pls care to leave a note so that others who follow will be able to learn better from SO.
Raj
Indeed. I upvoted the compensate. Creating a stub would solve the issue by avoiding it entirely (no use of Moq needed). It's a legit workaround and for those that voted this down, I'd encourage you to share why for the rest of the class.
Anderson Imes
Maybe Stackoverflowers don't like people with certifications? :)
Anderson Imes
A: 

Because of this problem I don't mock IUnityContainer, I use a real instance of UnityContainer instead. It's not ideal but I can test registration by checking that the container can resolve types as appropriate.

You can mock and use IServiceLocator when you're using it to resolve types in your classes, or even better, use register a factory with the container and use that instead.

GraemeF
+1  A: 

Have you tried mocking UnityBaseContainer or UnityContainer instead of IUnityContainer, ala this post by Rory Primrose? He is dealing with RhinoMocks but because I think the issue is related to Moq's internal use of Castle, you may be able to solve the problem this way.

Brett Bim