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
I found that Typemock doesn't return consistent result with ReturnRecursiveFakes when comes to List<string> and string, even though both are from mscorlib. It returns null for List, but return empty for string. By right it should either return null for both, or return empty for both.

Here's the code

    public class WorldString
    {
        public List<string> ReturnStringList()
        {
            throw new NotImplementedException();
        }

        public string ReturnString()
        {
            throw new NotImplementedException();
        }
    }

    [TestFixture, Isolated]
    public class Class1
    {
        private WorldString worldString;
        [SetUp]
        public void Init()
        {
            worldString = Isolate.Fake.Instance<WorldString>();
        }
        [Test, ClearMocks]
        public void WhenCallTest()
        {

            var rtrStrg = worldString.ReturnStringList();
            Assert.IsNull(rtrStrg);  //return null for list

           
        }

        [Test, ClearMocks]
        public void ReturnStringTest()
        {

            var rtrStrg = worldString.ReturnString();
            Assert.IsEmpty(rtrStrg);  //return empty for string

        }

    }


It seems that the unability to mock return List<string> value is restricted to ReturnRecursiveFakes only-- If I explicitly set a return value for ReturnStringList(), then I can still mock the method. Here's the code that will pass:

        [Test, ClearMocks]
        public void WhenCallMockTest()
        {

            Isolate.WhenCalled(() => worldString.ReturnStringList())
                .WillReturn(new List<string>());
            Assert.AreEqual(0, worldString.ReturnStringList().Count);

        }


Is this expected, or a bug?
________
Zx14 Vs Hayabusa
asked by nsoonhui (59.1k points)

1 Answer

0 votes
Hi Soon Hui,

Returning string.Empty instead of null is a special handling for string that was requested by our customers. Most people find it more intuitive when a recursive fake returns an empty string rather than a null, because they think of a string as a value rather than a reference. All reference types, however, return null - that's why lists return null rather than be instantiated as an empty list.
I think you found a great workaround for returning a reference to an actual list - just return it using WillReturn(). There's no problem with returning a real instance of a list, even though we can't create a fake list as it's an mscorlib type.

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