Moq + Castle动态代理 - 异常模拟嵌套通用接口

时间:2011-08-10 14:37:08

标签: moq aop rhino-mocks castle castle-dynamicproxy

我正在从Castle Dynamic Proxy接收一个参数异常,同时使用Moq创建一个对象模拟器,该模型使用具有接口约束的泛型方法实现嵌套通用接口。

例外是:System.ArgumentException:无法将父设置为接口。

在模拟创建后直接访问模拟的Object属性时发生。 (为了便于阅读,调用堆栈位于底部)

代码简单且自我描述:

    public interface A<T>
    {
        void Method<Z>() where Z : T; 
    }

    public interface B
    {
    }

    [Test]
    public void MockNestedGenericInterfaceTest()
    {
        Mock<A<B>> mock = new Mock<A<B>>();
        var o = mock.Object; //argument exception here
    }

如果删除Z:T子句,测试不会产生异常。

我做了一些研究并找到了一张票here。我正在使用最新版本的Moq和Castle。

这个问题有解决方法吗?我看到它工作的唯一方法是手动模拟实现。犀牛嘲笑也不适合我。

谢谢。

调用堆栈:

at System.Reflection.Emit.TypeBuilder.SetParent(Type parent)

在Castle.DynamicProxy.Generators.Emitters.GenericUtil.CopyGenericArguments(MethodInfo methodToCopyGenericsFrom,Dictionary 2 name2GenericType, ApplyGenArgs genericParameterGenerator) at Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.CopyGenericParametersFromMethod(MethodInfo methodToCopyGenericsFrom) at Castle.DynamicProxy.Generators.InvocationTypeGenerator.Generate(ClassEmitter class, ProxyGenerationOptions options, INamingScope namingScope) at Castle.DynamicProxy.Contributors.InterfaceProxyWithoutTargetContributor.GetInvocationType(MetaMethod method, ClassEmitter emitter, ProxyGenerationOptions options) at Castle.DynamicProxy.Contributors.InterfaceProxyWithoutTargetContributor.GetMethodGenerator(MetaMethod method, ClassEmitter class, ProxyGenerationOptions options, OverrideMethodDelegate overrideMethod) at Castle.DynamicProxy.Contributors.CompositeTypeContributor.ImplementMethod(MetaMethod method, ClassEmitter class, ProxyGenerationOptions options, OverrideMethodDelegate overrideMethod) at Castle.DynamicProxy.Contributors.CompositeTypeContributor.Generate(ClassEmitter class, ProxyGenerationOptions options) at Castle.DynamicProxy.Generators.InterfaceProxyWithoutTargetGenerator.GenerateType(String typeName, Type proxyTargetType, Type[] interfaces, INamingScope namingScope) at Castle.DynamicProxy.Generators.InterfaceProxyWithTargetGenerator.GenerateCode(Type proxyTargetType, Type[] interfaces, ProxyGenerationOptions options) at Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, IInterceptor[] interceptors) at Moq.Proxy.CastleProxyFactory.CreateProxy(ICallInterceptor interceptor, Type[] interfaces, Object[] arguments) at Moq.Mock 1.b__0() 在Moq.Mock 1.InitializeInstance() at Moq.Mock 1.OnGetObject() 在Moq.Mock`1.get_Object()

1 个答案:

答案 0 :(得分:3)

此错误已在最后一个版本的DynamicProxy中修复。确保您使用的是最新版本的Moq

相关问题