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
Try the code below all in the same .cs source file. By having all the code in the test assembly we eliminate from the equation the "internal members" not being visible in the test assembly for the purpose of this code example:

namespace TestProject1
{
    internal class MyItem
    {
        public MyItem(){}
    }

    internal class ClassToMock
    {
        public IList<MyItem> TheMyItemList { get; private set; }
        public IList<string> TheStringList { get; private set; }
    }

    internal class ClassUnderTest
    {
        public bool DoSomethingWithMyItemList(ClassToMock param)
        {
            return param.TheMyItemList.Count > 0; 
        }

        public bool DoSomethingWithStringList(ClassToMock param)
        {
            return param.TheStringList.Count > 0; 
        }
    }

    [TestClass]
    public class GenericsRecursiveFakeTest
    {
        [TestMethod]
        [Isolated]
        public void TestRecursiveFakeOnMyItemList()
        {
            ClassToMock fake = Isolate.Fake.Instance<ClassToMock>();

            ClassUnderTest target = new ClassUnderTest();

            Assert.IsFalse(target.DoSomethingWithMyItemList(fake));
        }

        [TestMethod]
        [Isolated]
        public void TestRecursiveFakeOnStringList()
        {
            ClassToMock fake = Isolate.Fake.Instance<ClassToMock>();

            ClassUnderTest target = new ClassUnderTest();

            Assert.IsFalse(target.DoSomethingWithStringList(fake));
        }
    }
}


In theory both tests should pass but TestRecursiveFakeOnMyItemList() fails because TheMyItemList returns null in return param.TheMyItemList.Count > 0;.

Now if I make MyItem public instead of internal, the test that was failing will pass!
I fail to see the logic of this... is it a bug in Typemock?
asked by patrick.gill@verint. (2.6k points)

4 Answers

0 votes
Patrick,

There's no reason this shouldn't work - actually, I tried it locally and it does work. What version of Isolator are you on? Can you send a small reproducing solution to support at typemock.com? I would like to understand the difference between our set ups.

Thanks,
Doron
Typemock Support
answered by doron (17.2k points)
0 votes
Doron, I sent the email with the solution reproducing the problem.
answered by patrick.gill@verint. (2.6k points)
0 votes
Hi Patrick,

The issue is that when creating fake values for interfaces (which happens when the recursive fake of ClassToMock is accessed), Isolator must have access to the types to instantiate. In your case, MyItem was internal and unaccessible, so constructing it failed. Isolator creates a dynamic assembly that contains all runtime generated types. In order to enable access to the internal types from it, you should add this to your assemblyinfo.cs:
[assembly: InternalsVisibleTo("DynamicMockAssembly")]


I hope this helps. Please let me know if this resolved your issue.

Thanks,
Doron
Typemock Support
answered by doron (17.2k points)
0 votes
Thanks, solution works.
answered by patrick.gill@verint. (2.6k points)
...