无法让 RhinoMocks 发出遵循通用类型限制规则的模拟
因此,使用 NUnit 和 RhinoMocks:
//Defines basic behavior of all persistable domain objects
public interface IDomainObject {...}
//defines domain objects specific to the Security DB
public interface ISecurityDomainObject : IDomainObject {...}
//Defines a basic transactional data Repository; there are multiple implementors
//which each close TRest to the interface that defines their DB's domain classes
public interface IRepository<TRest> : IDisposable where TRest:IDomainObject
{
IUnitOfWork BeginUnitOfWork();
void CommitUnitOfWork(IUnitOfWork unitOfWork);
void RollBackUnitOfWork(IUnitOfWork unitOfWork);
void Save<T>(T domainObject, IUnitOfWork unitOfWork) where T : class, TRest;
IQueryable<T> QueryFor<T>(IUnitOfWork unitOfWork) where T :class, TRest;
}
public interface ISecurityRepository:IRepository<ISecurityDomainObject> {}
public class SecurityRepository:ISecurityRepository
...
//This line breaks when run in an NUnit test
var securityRepository = MockRepository.GenerateMock<ISecurityRepository>();
...
我得到的错误是:
System.TypeLoadException : Method 'Save' on type 'ISecurityRepositoryProxyb8e21deb3cb04067a01ac5b63f7045af' from assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' tried to implicitly implement an interface method with weaker type parameter constraints.
at System.Reflection.Emit.TypeBuilder.TermCreateClass(RuntimeModule module, Int32 tk, ObjectHandleOnStack type)
at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
at System.Reflection.Emit.TypeBuilder.CreateType()
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.DynamicMock(Object[] argumentsForConstructor)
at Rhino.Mocks.MockRepository.<>c__DisplayClass7`1.<GenerateMock>b__6(MockRepository r)
at Rhino.Mocks.MockRepository.CreateMockInReplay(Func`2 createMock)
at Rhino.Mocks.MockRepository.GenerateMock(Object[] argumentsForConstructor)
at CSHD.Tests.Unit.Presentation.LoginTests.TestAuthenticationFails() in LoginTests.cs: line 138
当尝试针对具体类生成模拟时,我得到了类似的错误,这次是在 QueryFor() 方法上。如果我尝试重新定义 ISecurityRepository 接口中使用 TRest 的方法,则会收到“System.BadImageFormatException:尝试加载格式不正确的程序。(来自 HRESULT 的异常:0x8007000B)”,这看起来像是倒退了一步。
我认为核心问题是 RhinoMocks 对用作泛型类型限制的泛型参数感到困惑。我不知道它在哪里被混淆,因此我不知道如何或是否可以消除它的混淆。我有足够的集成测试覆盖率,如果我绝对有必要的话,我可以忽略这些失败的单元测试,但显然如果可以的话我宁愿修复它们。你的想法?
So, using NUnit and RhinoMocks:
//Defines basic behavior of all persistable domain objects
public interface IDomainObject {...}
//defines domain objects specific to the Security DB
public interface ISecurityDomainObject : IDomainObject {...}
//Defines a basic transactional data Repository; there are multiple implementors
//which each close TRest to the interface that defines their DB's domain classes
public interface IRepository<TRest> : IDisposable where TRest:IDomainObject
{
IUnitOfWork BeginUnitOfWork();
void CommitUnitOfWork(IUnitOfWork unitOfWork);
void RollBackUnitOfWork(IUnitOfWork unitOfWork);
void Save<T>(T domainObject, IUnitOfWork unitOfWork) where T : class, TRest;
IQueryable<T> QueryFor<T>(IUnitOfWork unitOfWork) where T :class, TRest;
}
public interface ISecurityRepository:IRepository<ISecurityDomainObject> {}
public class SecurityRepository:ISecurityRepository
...
//This line breaks when run in an NUnit test
var securityRepository = MockRepository.GenerateMock<ISecurityRepository>();
...
The error I get is:
System.TypeLoadException : Method 'Save' on type 'ISecurityRepositoryProxyb8e21deb3cb04067a01ac5b63f7045af' from assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' tried to implicitly implement an interface method with weaker type parameter constraints.
at System.Reflection.Emit.TypeBuilder.TermCreateClass(RuntimeModule module, Int32 tk, ObjectHandleOnStack type)
at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
at System.Reflection.Emit.TypeBuilder.CreateType()
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.DynamicMock(Object[] argumentsForConstructor)
at Rhino.Mocks.MockRepository.<>c__DisplayClass7`1.<GenerateMock>b__6(MockRepository r)
at Rhino.Mocks.MockRepository.CreateMockInReplay(Func`2 createMock)
at Rhino.Mocks.MockRepository.GenerateMock(Object[] argumentsForConstructor)
at CSHD.Tests.Unit.Presentation.LoginTests.TestAuthenticationFails() in LoginTests.cs: line 138
When attempting to generate the mock against the concrete class, I get a similar error, this time on the QueryFor() method. If I attempt to redefine the methods that use TRest in the ISecurityRepository interface, I get a "System.BadImageFormatException : An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)" which looks like a step backwards.
I think the core problem is that RhinoMocks is getting confused by the generic parameters being used as generic type restrictions. I have no clue exactly where it's being confused and therefore I don't know how or if I can unconfuse it. I have adequate integration test coverage that I could ignore these failing unit tests if I absolutely have to, but obviously I'd rather fix them if I can. Your thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来这是由
Castle.DynamicProxy
引起的已知问题,该问题已在该项目的最新主干中修复,但在最新的 Rhino Mocks 版本中仍然出现问题:http://groups.google.com/group/rhinomocks/browse_thread/thread/2c1b53bf66b77b8e/ad09a6cd1e304a93
如果您喜欢冒险您可以使用最新的 DynamicProxy 构建自己的 Rhino Mocks,它应该已修复。
It looks like this is a known issue caused by
Castle.DynamicProxy
that is fixed in the latest trunk of that project, but still broken in the latest Rhino Mocks release:http://groups.google.com/group/rhinomocks/browse_thread/thread/2c1b53bf66b77b8e/ad09a6cd1e304a93
If you're feeling adventurous you can build your own Rhino Mocks with the latest DynamicProxy and it should be fixed.
根据您定义通用参数的方式,Castle Dynamic Proxy(Rhino Mocks 用于生成代理)似乎没有正确生成代理类。如果您像这样定义 IRepository,则可以生成代理(因此是模拟):
如果您确实需要以其他方式定义它,则必须使用 Rhino Mocks 提交错误。
Looks like Castle Dynamic Proxy (which Rhino Mocks uses for proxy generation) is not generating the proxy class correctly given the way that you've defined your generic arguments. You can generate a proxy (and hence a mock) if you define your IRepository like this instead:
If you really need it defined the other way, you'll have to file a bug with Rhino Mocks.