Myles McDonnell
Myles McDonnell

Reputation: 13335

Two dimensional object array return type - NSubstitute

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?

Upvotes: 4

Views: 1006

Answers (2)

David Sette
David Sette

Reputation: 753

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.

Upvotes: 0

payo
payo

Reputation: 4561

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

Upvotes: 4

Related Questions