c# - 无法让 RhinoMocks 发出遵循通用类型限制规则的模拟

标签 c# rhino-mocks

因此,使用 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 接口(interface)中使用 TRest 的方法,我会收到“System.BadImageFormatException:尝试加载格式不正确的程序。(HRESULT 异常:0x8007000B)”这看起来像是倒退了一步.

我认为核心问题是 RhinoMocks 被用作泛型类型限制的泛型参数弄糊涂了。我不知道它到底在哪里被混淆了,因此我不知道如何或是否可以消除它的混淆。我有足够的集成测试覆盖率,如果绝对必要,我可以忽略这些失败的单元测试,但显然我宁愿尽可能修复它们。你的想法?

最佳答案

看起来这是一个由 CaSTLe.DynamicProxy 引起的已知问题,该问题已在该项目的最新主干中修复,但在最新的 Rhino Mocks 版本中仍然存在问题:

http://groups.google.com/group/rhinomocks/browse_thread/thread/2c1b53bf66b77b8e/ad09a6cd1e304a93

如果您喜欢冒险,可以使用最新的 DynamicProxy 构建您自己的 Rhino Mocks,它应该已修复。

关于c# - 无法让 RhinoMocks 发出遵循通用类型限制规则的模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4382624/

相关文章:

c# - 在回调方法中模拟参数

tdd - RhinoMocks : Correct way to mock property getter

c# - Rhino-Mocks 如何 mock ?

c# - EF 代码优先 - 超时已过。完成前超时时间已过

c# - WPF API 能否在 WCF 服务中安全使用?

c# - 从规范化表中获取数据

c# - SQL Server 查询 Unicode 字符串

C# 赌场程序作业

mocking - 为什么接口(interface)首选模拟?

c# - Rhino Mocks,每次执行 stub 方法时返回不同的结果