How can I test blades in MVC Turbine with Rhino Mocks?
I'm trying to set up blade unit tests in an MVC Turbine-derived site. The problem is that I can't seem to mock the IServiceLocator
interface without hitting the following exception:
System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
at System.Reflection.Emit.TypeBuilder._TermCreateClass(Int32 handle, Module module)
at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
at System.Reflection.Emit.TypeBuilder.CreateType()
at Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.BuildType()
at Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.BuildType()
at Castle.DynamicProxy.Generators.InterfaceProxyWithTargetGenerator.GenerateCode(Type proxyTargetType, Type[] interfaces, ProxyGenerationOptions options)
at Castle.DynamicProxy.DefaultProxyBuilder.CreateInterfaceProxyTypeWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options)
at Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyT开发者_JAVA技巧ypeWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options)
at Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, IInterceptor[] interceptors)
at Rhino.Mocks.MockRepository.MockInterface(CreateMockState mockStateFactory, Type type, Type[] extras)
at Rhino.Mocks.MockRepository.CreateMockObject(Type type, CreateMockState factory, Type[] extras, Object[] argumentsForConstructor)
at Rhino.Mocks.MockRepository.Stub(Type type, Object[] argumentsForConstructor)
at Rhino.Mocks.MockRepository.<>c__DisplayClass1`1.<GenerateStub>b__0(MockRepository repo)
at Rhino.Mocks.MockRepository.CreateMockInReplay<T>(Func`2 createMock)
at Rhino.Mocks.MockRepository.GenerateStub<T>(Object[] argumentsForConstructor)
at XXX.BladeTest.SetUp()
Everything I search for regarding this error leads me to 32-bit vs. 64-bit DLL compilation issues, but MVC Turbine uses the service locator facade everywhere and we haven't had any other issues, just with using Rhino Mocks to attempt mocking it.
It blows up on the second line of this NUnit set up method:
IRotorContext _context;
IServiceLocator _locator;
[SetUp]
public void SetUp()
{
_context = MockRepository.GenerateStub<IRotorContext>();
_locator = MockRepository.GenerateStub<IServiceLocator>();
_context.Expect(x => x.ServiceLocator).Return(_locator);
}
Just a quick aside; I've tried implementing a fake implementing IServiceLocator
, thinking that I could just keep track of calls to the type registration methods. This won't work in our setup, because we extend the service locator's interface in such a way that if the type isn't Unity-based, the registration logic is not invoked.
This has been fixed in Moq v4.0 beta. The issue was in Castle DynamicProxy 2.1 when creating dynamic proxies for interfaces with generic constraints.
http://code.google.com/p/moq/issues/detail?id=177
Yes, I've ran into the same issue with RhinoMocks while testing the runtime for Turbine also. I hate to say it but I worked around the issue by providing my own fake for IServiceLocator for where I needed, but as you explained, you can't do that. :(
I'm not following this piece from your question, "if the type isn't Unity-based"??
精彩评论