
moq - issue #311
mock indexer with 'ref object' as parameter - IndexOutOfRangeException
What steps will reproduce the problem? 1. i'm trying to write the mock unit test for my WIA implementation. After reference the WIA component, the visual studio 2010 genereted these classes for me:
namespace WIA { [Guid("40571E58-A308-470A-80AA-FA10F88793A0")] [TypeLibType(4160)] public interface IProperties : IEnumerable { [DispId(1)] int Count {get; }
[DispId(0)]
Property this[ref object Index] {get; }
[DispId(2)]
bool Exists(ref object Index);
[TypeLibFunc(1)]
[DispId(-4)]
IEnumerator GetEnumerator();
}
} namespace WIA { [CoClass(typeof(PropertiesClass))] [Guid("40571E58-A308-470A-80AA-FA10F88793A0")] public interface Properties : IProperties { } }
Properties is a collection of Property object, the collection is index by String; therefore, to mock the Properties object, i have
var mockWIAProperties = new Mock<WIA.Properties>();
mockWIAProperties.SetupGet(m => m[It.IsAny<String>()]).Returns(getMockWIAProperty());
And the getMockWIAProperty is:
public static WIA.Property getMockWIAProperty()
{
var mockWIAProperty = new Mock<WIA.Property>();
return mockWIAProperty.Object;
}
2. 3.
What is the expected output? Should be no error at run time
What do you see instead? failed: System.IndexOutOfRangeException : Index was outside the bounds of the array
What version of the product are you using? On what operating system? Moq - 4.0.10827.0
Please provide any additional information below.
Comment #1
Posted on Feb 8, 2012 by Happy BearI too get the same error "System.IndexOutOfRangeException: Index was outside the bounds of the array." when I mock Microsoft.Office.Interop.Word.Shapes and trying to setup
object mockintobj = 1; mockShapes.Setup(x => x[mockintobj]).Returns(mockShape.Object);
Here is the parameter expected by Microsoft.Office.Interop.Word.Shape indexer
Shape this[ref object Index] {get; }
Is there any fix scheduled in future?
Status: New
Labels:
Type-Defect
Priority-Medium