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,

The 5.3.0 release almost got generics right. I've found one bug that is triggered in case expectation is set multiple times on the same method. Here's the code:

        public static class DataHelper
        {
            public static LT Fill<T>()
                where LT : ICollection<T>, new()
            {
                return new LT();
            }
        }

        [Test]
        public void Test_Succeeds()
        {
            var fakeStrings = new List<string>();
            fakeStrings.Add("abc");

            Isolate.WhenCalled(() => DataHelper.Fill<string, List<string>>()).WillReturn(fakeStrings);

            var strings = DataHelper.Fill<string, List<string>>();
            Assert.IsNotNull(strings);
            Assert.AreNotEqual(0, strings.Count);
        }

        [Test]
        public void Test_Fails()
        {
            var fakeStrings = new List<string>();
            fakeStrings.Add("abc");

            Isolate.WhenCalled(() => DataHelper.Fill<string, List<string>>()).WillReturn(fakeStrings);
            Isolate.WhenCalled(() => DataHelper.Fill<string, List<string>>()).WillReturn(fakeStrings);

            var strings = DataHelper.Fill<string, List<string>>();
            Assert.IsNotNull(strings);
            Assert.AreNotEqual(0, strings.Count);
        }


As you can see, the only difference between the successful and failed test is that DataHelper.Fill is faked twice in failed test. But this breaks the list that it returns: the return value is an empty list instead of a list containing string "abc".
asked by vagif (19.4k points)

8 Answers

0 votes
There are more tests that are now broken. I did not have time to recreate them using simple examples, but they all have common pattern:
- They use generics in method signature;
- There are more than one fakes created on such methods (often using different specialization).

For example, the test with the code fails:

            var fake = Isolate.Fake.Instance<CpLanguages>();
            Isolate.WhenCalled(() => CpGlobal<CpGlobalProperties>.Languages).WillReturn(fake);
            Isolate.WhenCalled(() => CpGlobal<CpGlobalProperties>.LanguageNames(null)).WillReturn(fake);


But a similar test with only one WhenCalled succeeded. In fact all tests that have multiple WhenCalled applied to methods with generics fail now.
answered by vagif (19.4k points)
0 votes
Hi Vagif,

This should obviously work - we will investigate it and get back to you on this forum.

Doron
Typemock Support
answered by doron (17.2k points)
0 votes
Hi Vagif

I checked the example you gave and it seems like a bug in generic static methods.
The workaround for the moment is to use:
Isolate.Fake.StaticMethods(typeof(DataHelper));
This fixed the example you gave.
Can you please try it in the other tests and see if it works?

Thanks for the report.
answered by ohad (35.4k points)
0 votes
Well done! Your workaround worked for all broken tests. I re-enabled them. Thanks a lot!
answered by vagif (19.4k points)
0 votes
This issue still seems to occur in Typemock 5.3.4, when calling static methods without generics:

Isolate.WhenCalled(()=>Config.GetSetting("mysetting")).WithExactArguments().WillReturn("hello");


Without any other mocking fails. This, however, works:

Isolate.Fake.StaticMethods(typeof(Config));
Isolate.WhenCalled(()=>Config.GetSetting("mysetting")).WithExactArguments().WillReturn("hello");
answered by zcrar70 (2.9k points)
0 votes
zcrar70,

How does this fail for you? What error message/behavior are you getting?

Can you post the code for the Config class?

Thanks,
Doron
Typemock Support
answered by doron (17.2k points)
0 votes
Hi,

The error I get when omitting 'Isolate.Fake.StaticMethods(typeof(Config))' is just that the underlying method gets called.

I don't really want to post the Config class here, but I don't seem to be able to reproduce it with a test class with similar characteristics; so it definitely semes like something related to that specific class as opposed to static methods in general. I'll have another go and post back here if I get anywhere with reproducing this with a test class.

Thanks,
Nick
answered by zcrar70 (2.9k points)
0 votes
Thanks, Nick.

Also, can you try an upgrade to the latest version (5.3.4)? It contains a few improvements around generics handling which may affect this issue.

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