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
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
I understand mscorlib faking support is triaged according to needs. I have to say, whenever I need to fake a class that inherits any of the mscorlib IEnumerable types and is used in a foreach, it's a pain. Being able to use the WithReturnCollectionValuesOf with mscorlib enumerables or classes that inherit them would be grand.
Cheers!
asked by Guillaume (3.5k points)

3 Answers

0 votes
Hi Guillaume,

mscorlib types faking is usually added by request.

Please describe the scenario in which you need this feature and maybe how you would expect to use it (how the API should look).
A small description with pseudo code would do.
answered by alex (18k points)
0 votes
Hi alex!
Here's a very contrived example. I can't change the code in ThirdParty. As a matter of fact, the only way to obtain Class1 is through ListOfClass1.

using System.Collections.Generic;
using NUnit.Framework;
using ThirdParty;
using TypeMock.ArrangeActAssert;

namespace ThirdParty
{
    public class Class1
    {
    }

    public class ListOfClass1 : List<Class1>
    {
        public static ListOfClass1 Load()
        {
            //Do some magic.
            return new ListOfClass1();
        }
    }
}

namespace MyCode
{
    public class MyClass
    {
        public void DoSomething()
        {
            var list = ListOfClass1.Load();
            foreach (var class1 in list)
            {
                //do something
            }
        }
    }
}

namespace MyTests
{
    [TestFixture]
    [Isolated]
    public class Test
    {
        [Test]
        public void TestSomething()
        {
            var firstFake = Isolate.Fake.Instance<Class1>();
            var secondFake = Isolate.Fake.Instance<Class1>();
            var fakeList = Isolate.Fake.Instance<ListOfClass1>();

            //Woops, doesn't work.
            Isolate.WhenCalled(() => fakeList).WillReturnCollectionValuesOf(new List<Class1> {firstFake, secondFake});

            var didItWork = false;
            foreach (var class1 in fakeList)
            {
                didItWork = true;
            }
            Assert.IsTrue(didItWork);
        }
    }
}


edit: modified code to more closely reflect my usage.
answered by Guillaume (3.5k points)
0 votes
Hi Guillaume,

It confused us a bit. While Isolator cannot fake objects within mscorlib, like List, it can definitely return them. So according to your code, you can replace the real ListOfClass1, with another real one, but initialized differently.

So a test will look like this:
[TestMethod]
public void ReturnListFromLoad()
{
    // Set up a real ListOfClass1, containing fake Class1 objects 
    var firstFake = Isolate.Fake.Instance<Class1>();
    var secondFake = Isolate.Fake.Instance<Class1>();
    ListOfClass1 otherList = new ListOfClass1() {firstFake, secondFake};

    // Replace the returned list , the actual Load method doesn't get called.
    Isolate.WhenCalled(() => ListOfClass1.Load()).WillReturn(otherList);

    // Now run the under test code.
    var returnedlist = ListOfClass1.Load();
    var didItWork = false;
    foreach (var class1 in returnedlist)
    {
        didItWork = true;
    }
    Assert.IsTrue(didItWork);
}


Does this help?

Gil
answered by gilz (14.5k points)
...