【问题标题】:Moq + Castle Dynamic Proxy - exception mocking nested generic interfacesMoq + Castle Dynamic Proxy - 模拟嵌套通用接口的异常
【发布时间】:2011-10-24 03:53:43
【问题描述】:

我收到来自 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
    }

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

我做了一些研究,发现了一张票 here。我正在使用最新版本的 Moq 和 Castle。

是否有解决此问题的方法?我看到它工作的唯一方法是手动模拟实现。 Rhino mocks 也不适合我。

谢谢。

调用栈:

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

在 Castle.DynamicProxy.Generators.Emitters.GenericUtil.CopyGenericArguments(MethodInfo methodToCopyGenericsFrom, Dictionary2 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.Mock1.b__0() 在 Moq.Mock1.InitializeInstance() at Moq.Mock1.OnGetObject() 在 Moq.Mock`1.get_Object()

【问题讨论】:

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


    【解决方案1】:

    此错误已在 DynamicProxy 的最新版本中修复。确保您使用的是最新版本的起订量

    【讨论】:

    • 感谢您的反馈,但使用 Castle.Core.dll 2.5.2 版和 Moq.dll 4.0.0 版 - 不起作用。稍后我会提供一个测试项目来证明这一点。
    • 好的。我很确定该错误已修复,但也许不是 v2.5.2 而是主干...无论如何-失败的测试会很棒。
    • 好的,我已经在这里托管了测试项目 - link 对于我们当前的项目,我们将在一段时间内编译最新的城堡源。我会发布它是否有帮助。
    • 测试使用的是 Moq 的 DP 合并版本。这个问题在 Castle.Core 2.5.2 中得到了肯定的解决
    • 最新最小起订量使用 Castle.Core 的确切版本 - 2.5.0。我认为您必须求助于程序集绑定策略覆盖才能使其与 2.5.2 一起使用。我想知道是否可以更新 Moq 来解决这个问题: System.TypeInitializationException : 'Moq.Mock`1' 的类型初始化程序引发了异常。 ----> System.TypeInitializationException : 'Moq.Proxy.CastleProxyFactory' 的类型初始化程序引发了异常。 ----> System.IO.FileLoadException : 无法加载文件或程序集 'Castle.Core, Version=2.5.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc' 或其依赖项之一。
    猜你喜欢
    • 1970-01-01
    • 2013-11-08
    • 2011-09-05
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多