Friday, 5 February 2010

Mocking the Unity Container with Rhino Mocks

I’ve recently starting using the UnitContainer from Microsoft for my dependency injection framework.  One thing I came across right at the beginning was and error when I was trying to mock the interface.

   1: _Mocks = new MockRepository();
   2: _Container = _Mocks.StrictMock<IUnityContainer>();

Seems like the normal way to do it, but you’ll get this error:

An attempt was made to load a program with an incorrect format.

Difficult looking problem, simple solution.  I’m not sure if this will fix the issue for other mocking frameworks, but for Rhino Mocks all you need to do is mock the UnitContainerBase class instead of the interface like so:

_Mocks = new MockRepository();
_Container = _Mocks.StrictMock<UnityContainerBase>();

Seems simple enough, but I had a little trouble searching for the answer.