是什么导致了这个异常?我正在尝试Moq用来模拟Microsoft.Office.Interop.Excel.Range. 我想在Range里面嵌套另一个嘲笑。但是当我尝试访问嵌套异常时,会抛出异常。例外无法将 [] 索引应用于“Castle.Proxies.RangeProxy”类型的表达式代码[TestMethod]public void RangeProxyIndexTest(){ // creating first range var cell1 = new Moq.Mock<Range>(); cell1.Setup(c => c.Value2).Returns("1"); var range1Mock = new Moq.Mock<Range>(); range1Mock.SetupGet(r => r[1, Moq.It.IsAny<Object>()]).Returns(cell1.Object); var range1 = range1Mock.Object; // creating second range var cell2 = new Moq.Mock<Range>(); cell2.Setup(c => c.Value2).Returns("2"); var range2Mock = new Moq.Mock<Range>(); range2Mock.SetupGet(r => r[1, Moq.It.IsAny<Object>()]).Returns(cell2.Object); var range2 = range2Mock.Object; // merging both ranges into 1 var range3Mock = new Moq.Mock<Range>(); range3Mock.SetupGet(r => r[1, Moq.It.IsAny<Object>()]).Returns(range1); range3Mock.SetupGet(r => r[2, Moq.It.IsAny<Object>()]).Returns(range2); var range3 = range3Mock.Object; // testing succeeds Assert.AreEqual("1", range1[1].Value2); Assert.AreSame(range1, range3[1]); Assert.AreEqual("2", range2[1].Value2); Assert.AreSame(range2, range3[2]); // exceptions are thrown here Assert.AreEqual("1", range3[1][1].Value2); Assert.AreEqual("2", range3[2][1].Value2);}
1 回答
胡子哥哥
TA贡献1825条经验 获得超6个赞
虽然情况不同,但这个异常的原因和你打开的另一个问题完全一样。
将您的验证部分更改为:
Assert.AreEqual("1", (range3[1] as Range)[1].Value2); Assert.AreEqual("2", (range3[2] as Range)[1].Value2);
- 1 回答
- 0 关注
- 214 浏览
添加回答
举报
0/150
提交
取消