Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Moq for an indexer property causes InvalidCastException from Castle
    primarykey
    data
    text
    <p>When trying to use Moq to setup an indexer property, the underlying Castle library throws an InvalidCastException. I am using Moq 4.0.10827, the details follow. Thanks in advance if anyone can help me get the simple test (below) to run. I am rewriting unit tests with Moq and I am trying to re-do the test for the following method:</p> <pre><code>public INetwork GetInputNetwork(IInputPortMgr inPortMgr) { var port = inPortMgr[0]; return port.InputNetwork; } </code></pre> <p>The IInputPortMgr interface is as follows:</p> <pre><code>public interface IInputPortMgr { IInputPort this[int index] { get; } } </code></pre> <p>The test I wrote (and tried numerous variations) can be summarized by:</p> <pre><code>[Test] public void GetInputNetwork_Returns_InputNetwork_From_InputPort() { var mockInPortMgr = new Mock&lt;IInputPortMgr&gt;(); var mockInPort = new Mock&lt;IInputPort&gt;(); var mockNet = new Mock&lt;INetwork&gt;(); mockInPortMgr.Setup(m =&gt; m[0]).Returns(mockInPort.Object); // exception here mockInPort.Setup(m =&gt; m.InputNetwork).Returns(mockNet.Object); // Assertions Here } </code></pre> <p>But when run the unit test fails because an exception is thrown by the line</p> <p><code>mockInPortMgr.Setup(m =&gt; m[0]).Returns(mockInPort.Object);</code></p> <p>The details of the exception are:</p> <pre><code>System.InvalidCastException : Unable to cast object of type 'System.Collections.ObjectModel.ReadOnlyCollection`1[System.Reflection.CustomAttributeTypedArgument]' to type 'System.Array'. at System.Reflection.Emit.CustomAttributeBuilder.EmitValue(BinaryWriter writer, Type type, Object value) at System.Reflection.Emit.CustomAttributeBuilder.InitCustomAttributeBuilder(ConstructorInfo con, Object[] constructorArgs, PropertyInfo[] namedProperties, Object[] propertyValues, FieldInfo[] namedFields, Object[] fieldValues) at Castle.DynamicProxy.AttributeUtil.CreateBuilder(CustomAttributeData attribute) at Castle.DynamicProxy.AttributeUtil.&lt;GetNonInheritableAttributes&gt;d__0.MoveNext() at Castle.DynamicProxy.Generators.MetaProperty.BuildPropertyEmitter(ClassEmitter classEmitter) at Castle.DynamicProxy.Contributors.CompositeTypeContributor.ImplementProperty(ClassEmitter emitter, MetaProperty property, ProxyGenerationOptions options) 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.&lt;InitializeInstance&gt;b__0() at Moq.Mock`1.InitializeInstance() at Moq.Mock`1.OnGetObject() at Moq.Mock`1.get_Object() at Tests.Psi.Type6.Fx.Type6Fixture.GetInputNetwork() in Type6Fixture.cs: line 293 </code></pre> <p>Any ideas or could anyone point me in the right direction? Thanks.</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