Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you Mock IUnityContainer?

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!

like image 403
Ryan Rauh Avatar asked Mar 16 '09 16:03

Ryan Rauh


2 Answers

You don't.

The only reason to mock the container is if you're passing it around. That's an anti-pattern.

Instead you want to compose the entire object graph at the application's entry point, or Composition Root.

If you need to create instances on the fly, use Automatic Factories.

For your tests, you can either construct the object under test and pass mock objects to the constructor or create a new container in the test and register mock objects with it.

like image 103
TrueWill Avatar answered Oct 18 '22 09:10

TrueWill


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.

like image 40
Nathan Southerland Avatar answered Oct 18 '22 08:10

Nathan Southerland