如何使用Rhino Mocks在MVC Turbine中测试刀片?

时间:2010-02-24 15:13:40

标签: asp.net-mvc unit-testing nunit rhino-mocks turbine

我正在尝试在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);
}

快一点;我已经尝试实现一个虚假的实现IServiceLocator,认为我可以跟踪对类型注册方法的调用。这在我们的设置中不起作用,因为我们扩展服务定位器的接口,如果类型不是基于Unity的,则不调用注册逻辑。

2 个答案:

答案 0 :(得分:1)

是的,我在测试Turbine的运行时也遇到了与RhinoMocks相同的问题。我不想这么说但是我通过在我需要的地方提供我自己的假装IServiceLocator来解决这个问题,但正如你所解释的那样,你不能这样做。 :(

我不是在你的问题中关注这一部分,“如果类型不是基于Unity的”??

答案 1 :(得分:1)

这已在Moq v4.0 beta中修复。在为具有通用约束的接口创建动态代理时,问题出现在Castle DynamicProxy 2.1中。

http://code.google.com/p/moq/issues/detail?id=177