Note that there are some explanatory texts on larger screens.

plurals
  1. POMoq + Castle Dynamic Proxy - exception mocking nested generic interfaces
    primarykey
    data
    text
    <p>I'm receiving an argument exception from Castle Dynamic Proxy, while using Moq to create a mock of object that is implementing a nested generic interface with generic method that has an interface constraint.</p> <p>The exception is: System.ArgumentException : Cannot set parent to an interface.</p> <p>Happens while accessing Object property of the mock directly after the mock creation. (Call stack is at the bottom for the sake of readability)</p> <p>The code is simple and self-describing:</p> <pre><code> public interface A&lt;T&gt; { void Method&lt;Z&gt;() where Z : T; } public interface B { } [Test] public void MockNestedGenericInterfaceTest() { Mock&lt;A&lt;B&gt;&gt; mock = new Mock&lt;A&lt;B&gt;&gt;(); var o = mock.Object; //argument exception here } </code></pre> <p>Test does not generate an exception if where Z : T clause is removed.</p> <p>I did some research and found a ticket <a href="http://code.google.com/p/moq/issues/detail?id=259" rel="nofollow" title="here">here</a>. I'm using latest versions of Moq and Castle.</p> <p>Is there a workaround for this problem? The only way I see it working is a manual mock implementation. Rhino mocks did not work for me either.</p> <p>Thank you.</p> <p>Call stack:</p> <pre><code>at System.Reflection.Emit.TypeBuilder.SetParent(Type parent) </code></pre> <p>at Castle.DynamicProxy.Generators.Emitters.GenericUtil.CopyGenericArguments(MethodInfo methodToCopyGenericsFrom, Dictionary<code>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</code>1.b__0() at Moq.Mock<code>1.InitializeInstance() at Moq.Mock</code>1.OnGetObject() at Moq.Mock`1.get_Object() </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload