chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Hi,

I can't mock element inside arrays. Here's the class code
    public class ArrayContainer
    {
        public int MyVariable
        {
            get;
            set;
        }
        public AnElement[] MyContainer
        {
            get;
            set;
        }
    }

    public class AnElement
    {
        public int Variables
        {
            get;
            set;
        }
    }


Here's the test code:
    [TestFixture, ClearMocks]
    public class TypemockTest
    {
        public TypemockTest()
        {

        }

        [Test, Isolated]
        public void TestArray()
        {
            ArrayContainer container = new ArrayContainer();
            Isolate.WhenCalled(() => container.MyVariable).WillReturn(20);
            Assert.AreEqual(20, container.MyVariable);

            Isolate.WhenCalled(() => container.MyContainer).WillReturn(new AnElement[2]);
            Isolate.WhenCalled(() => container.MyContainer[0]).WillReturn(new AnElement() { Variables = 20 });
            Assert.AreEqual(20, container.MyContainer[0].Variables);
        }
    }


And here's the exception:
failed: TypeMock.TypeMockException : 
*** Method get_MyContainer in type Esteem.Tests.ModuleData.ArrayContainer returns Esteem.Tests.ModuleData.AnElement[] and not Esteem.Tests.ModuleData.AnElement.
   at TypeMock.RecordExpectations.a(Object A_0)
   at TypeMock.RecordExpectations.Return(Object returnValue)
   at TypeMock.ArrangeActAssert.Recorder`1.a(T A_0)
   C:developmentEsteem7 .Net2ModuleDataUnitTestTypemockTest.cs(28,0): at Esteem.Tests.ModuleData.TypemockTest.TestArray()
   at TypeMock.MethodDecorator.CallRealMethod()
   at TypeMock.DecoratorAttribute.CallDecoratedMethod()
   at TypeMock.ArrangeActAssert.IsolatedAttribute.Execute()
   at TypeMock.DecoratorAttribute.CallDecoratedMethod()
   at TypeMock.ClearMocksAttribute.Execute()
   at TypeMock.MethodDecorator.e()
   at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
   at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)


I think TypeMock is confused when I call
Isolate.WhenCalled(() => container.MyContainer[0]).WillReturn(new AnElement() { Variables = 20 });

It thinks that the container.MyContainer[0] is AnElement Type, not an array of AnElement.
________
Ford Model A (1903) history
asked by nsoonhui (59.1k points)

1 Answer

0 votes
Hi
It seems like you have found a bug. :oops:
Thanks for reporting it.
answered by ohad (35.4k points)
...