Mocking Unity

75 views
Skip to first unread message

Emanuele DelBono

unread,
Aug 12, 2008, 2:51:25 AM8/12/08
to Rhino...@googlegroups.com
Today I was playing with composite WPF and in a test I need to mock
the IUnitiyContainer interface.
I wrote this code:

[Test]
public void JustMockTheContainer()
{
MockRepository mockRepository = new MockRepository();

IUnityContainer container = mockRepository.Stub<IUnityContainer>();
}

This test fail!
The rasied exception is:

MbUnit.ReSharperRunner.MbUnit.RunnerException:
System.BadImageFormatException: An attempt was made to load a program
with an incorrect format. (Exception from HRESULT: 0x8007000B)


And looking at the stack trace it seems that the method that raise the
exception is at
Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.BuildType()
called by Rhino.Mocks.MockRepository.MockInterface(CreateMockState
mockStateFactory, Type type, Type[] extras)

I'm using RhinoMocks 3.5 and mbUnit 2.4
Any ideas?


--
ema
http://blog.codiceplastico.com

Shawn Neal

unread,
Aug 12, 2008, 10:12:45 AM8/12/08
to Rhino...@googlegroups.com
The only time I've seen that is when I was using a .NET IL encryptor that was doing who knows what to my code. 

Does this happen when mocking other interfaces besides unity?  I did find this blog post that might be relevant.

Emanuele DelBono

unread,
Aug 12, 2008, 10:25:57 AM8/12/08
to Rhino...@googlegroups.com
Thanks,
In fact the error is quite strange, I'm using the .NET Fx 3.5 with
RhinoMock 3.5 so I don't thik that there are compatibility issues.
The error appears only when I Mock the IUnityContainer interface.

:-|

bye
emanuele

--
ema
http://blog.codiceplastico.com

Shawn Neal

unread,
Aug 12, 2008, 1:59:35 PM8/12/08
to Rhino...@googlegroups.com
Moq apparently has the same problem, as they also use Castle dynamic proxy.  I have no idea what the actual problem is though.

Ayende Rahien

unread,
Aug 12, 2008, 6:47:19 PM8/12/08
to Rhino...@googlegroups.com
The likely issue are methods like this:

IUnityContainer RegisterType<TFrom, TTo>(string name) where TTo : TFrom;

The problem is that there is a bug in the System.Reflection.Emit implementation which cause the runtime to emit invalid code for this scenario.
Reply all
Reply to author
Forward
0 new messages