Two dimensional object array return type - NSubstitute

痴心易碎 提交于 2019-12-22 08:37:12

问题


I get a cast exception

System.InvalidCastException : Unable to cast object of type 'System.Object[]' to type 'System.Object[,]'. at Castle.Proxies.ITestProxy.Get2DArray() at Scratch.TestFixture.Get2DArray() in TestTest.cs: line 17

from from the below:

[TestFixture]
public class TestFixture
{
    [Test]
    public void Get2DArray()
    {
        Substitute.For<ITest>().Get2DArray().Returns(new object[1,1]);
    }
}

public interface ITest
{
    object[,] Get2DArray();
}

can anyone throw any light on this? I'm thinking it's a NSubstitute bug?


回答1:


NSubstitute depends on Castle, which depends on Reflection.Emit, so they blame Reflection.Emit.

http://issues.castleproject.org/issue/DYNPROXY-154

For a workaround to your problem, looks like you cannot use multidimensional arrays. Note that your exception actually occurs on Get2DArray(), not Returns.

Please note that I, personally, am working on a mocking framework that does NOT use Reflection.Emit (nor Castle for that matter) It's going to take a few weeks before even an Alpha is ready, but it is quite a powerful tool. There are many scenarios Castle fails that I don't (my site will list these). If you are interested, please follow http://smug.codeplex.com




回答2:


This may be a bit late, but could help someone who encounters this issue and comes across this question.

We found a way around this limitation by having your Interface return Array instead of object[,].

There is an implicit conversion, so the code inside your implementation of the interface should be able to remain the same.



来源:https://stackoverflow.com/questions/9912368/two-dimensional-object-array-return-type-nsubstitute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!