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!

Here's strange one.

public class MyItem
    {
        public int Prop1 { get; set; }

        public int Prop2 { get; set; }
    }

public class DifferentMethods
    {
        public void MethodNoParams()
        {
        }

        public void MethodOneParam(int a)
        {
        }

        public void MethodTwoParams(DateTime a, int b)
        {
        }

        public void MethodThreeParams(int a, DateTime b, IList<MyItem> c)
        {
        }
    }

    public class UnderTest
    {
        public DifferentMethods Methods { get; set; }

        public void TestMe()
        {
            Methods.MethodNoParams();

            Methods.MethodOneParam(1);

            Methods.MethodTwoParams(DateTime.Now, 1);

            List<MyItem> list = new List<MyItem>();
            list.Add(new MyItem());
            list.Add(new MyItem());

            Methods.MethodThreeParams(1, DateTime.Today, list);
        }
    }


And here's a test:
[TestClass()]
    public class UnderTestTest
    {
        [TestMethod]
        [Isolated]
        public void TestMeTest()
        {
            DateTime dateStub = DateTime.Now;

            DifferentMethods methodsFake = Isolate.Fake.Instance<DifferentMethods>(Members.MustSpecifyReturnValues);

            Isolate.WhenCalled(() => methodsFake.MethodNoParams())
                .IgnoreCall();

            Isolate.WhenCalled((int i) => methodsFake.MethodOneParam(i))
                .AndArgumentsMatch((i) => i == 1)
                .WithExactArguments()
                .IgnoreCall();

            Isolate.WhenCalled((DateTime date) => methodsFake.MethodTwoParams(date, 1))
                .AndArgumentsMatch((date) => date.Date == dateStub.Date)
                .WithExactArguments()
                .IgnoreCall();

            Isolate.WhenCalled((IList<MyItem> list) => methodsFake.MethodThreeParams(1, DateTime.Today, list))
                .AndArgumentsMatch((list) =>
                    {
                        return true;//list.Count == 2;
                    })
                .WithExactArguments()
                .IgnoreCall();

            UnderTest instance = new UnderTest();
            instance.Methods = methodsFake;
            instance.TestMe();
        }
    }


The test throws

System.IndexOutOfRangeException: Index was outside the bounds of the array.
at ca.a(Object[] A_0)
at ca.f(Object[] A_0)
at ca.b(Object[] A_0, Type A_1)
at c3.c(Object[] A_0, Type A_1)
at c3.a(Object[] A_0, Type A_1)
at c3.b(Object[] A_0, Type A_1)
at ad.a(Object A_0, Object[] A_1, Type A_2, Scope A_3, Int32 A_4, Object A_5, Type A_6)
at bh.a(String A_0, Object[] A_1, Object A_2, Object A_3, String A_4, Type A_5)
at b4.a(String A_0, Object A_1, MethodBase A_2, Object[] A_3, Object A_4, String A_5, bh A_6)
at b4.b(Object A_0, String A_1, String A_2, MethodBase A_3, Object[] A_4, Object A_5)
.... at PlayingWithAAA.DifferentMethods.MethodOneParam(Int32 a)

So it seems like the bug in expectation for MethodOneParam(Int32 a). But if I comment out expectation for MethodThreeParams(...) the test passes. Also if I comment out expectations for both MethodOneParam(...) and MethodTwoParams(...) it passes. They don't like each other some way. I can't figure.

10x
asked by 3ter (2.5k points)

3 Answers

0 votes
Hi,

Yes, it is strange.
We've reproduce it here with your code, so thank you. We'll explore it and get back to you.
answered by gilz (14.5k points)
0 votes
Here is a small update:

I added another method to DifferentMethods with the same parameters in another order:
public void MethodThreeParams3(DateTime a, IList<MyItem> b, byte c)
        {
        }

and call only 2 methods
Methods.MethodThreeParams(1, DateTime.Today, list);
Methods.MethodThreeParams3(DateTime.Today, list, 6);

in UnderTest and expect them with CustomChecker:

Isolate.WhenCalled((IList<MyItem> list) => methodsFake.MethodThreeParams(1, DateTime.Today, list))
                .AndArgumentsMatch((list) =>
                    {
                        return true;//list.Count == 2;
                    })
                .WithExactArguments()
                .IgnoreCall();
            
 Isolate.WhenCalled((IList<MyItem> list) => methodsFake.MethodThreeParams3(DateTime.Today, list, 6))
                .AndArgumentsMatch((list) =>
                {
                    return true;//list.Count == 2;
                })
                .WithExactArguments()
                .IgnoreCall();

Now I get another exception:
TestCase 'UnderTestUnitTests.UnderTestTest.TestMeTest'
failed: System.ArgumentException: Object of type 'System.DateTime' cannot be converted to type 'System.Collections.Generic.IList`1[PlayingWithAAA.MyItem]'.
at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Delegate.DynamicInvoke(Object[] args)
at ca.f(Object[] A_0)
at ca.b(Object[] A_0, Type A_1)
at c3.c(Object[] A_0, Type A_1)
at c3.a(Object[] A_0, Type A_1)
at c3.b(Object[] A_0, Type A_1)
at ad.a(Object A_0, Object[] A_1, Type A_2, Scope A_3, Int32 A_4, Object A_5, Type A_6)
at bh.a(String A_0, Object[] A_1, Object A_2, Object A_3, String A_4, Type A_5)
at b4.a(String A_0, Object A_1, MethodBase A_2, Object[] A_3, Object A_4, String A_5, bh A_6)
at b4.b(Object A_0, String A_1, String A_2, MethodBase A_3, Object[] A_4, Object A_5)
...: at PlayingWithAAA.DifferentMethods.MethodThreeParams(Int32 a, DateTime b, IList`1 c)

However, I can call the same method several times and expectations work fine. For me it seems like Isolator gets confused in different methods' signatures.
answered by 3ter (2.5k points)
0 votes
3ter,

This seems like a bug. I'm going to debug this and get back to you with an answer. Let's take this discussion offline - I'll email you to follow up.

Doron
Typemock Support
answered by doron (17.2k points)
...