我正在尝试在 MVC Turbine 衍生站点中设置刀片单元测试。问题是我似乎无法在IServiceLocator
不遇到以下异常的情况下模拟界面:
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.CreateInterfaceProxyTypeWithoutTarget(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()
我搜索的有关此错误的所有内容都导致我遇到 32 位与 64 位 DLL 编译问题,但 MVC Turbine 到处都使用服务定位器外观,我们没有遇到任何其他问题,只是使用 Rhino Mocks 尝试模拟它.
它在这个 NUnit 设置方法的第二行爆炸了:
IRotorContext _context;
IServiceLocator _locator;
[SetUp]
public void SetUp()
{
_context = MockRepository.GenerateStub<IRotorContext>();
_locator = MockRepository.GenerateStub<IServiceLocator>();
_context.Expect(x => x.ServiceLocator).Return(_locator);
}
顺便说一句;我试过实现一个假的 implementation IServiceLocator
,认为我可以跟踪对类型注册方法的调用。这在我们的设置中不起作用,因为我们扩展了服务定位器的接口,如果类型不是基于 Unity 的,则不会调用注册逻辑。