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 get a NullReferenceException with Isolator 5.1.1 and 5.2.3 when I instantiate a custom generic list after setting an expectation on an object that returns a instance of the custom generic list.

Thee simplest way I could reproduce the bug is with the following code:

[Test]
public void Test()
{
PersonService personService = Isolate.Fake.Instance<PersonService>();

Isolate.WhenCalled(() => personService.ServiceMethod()).WillReturn(null);

// NullReferenceException if generic type != Person
var filterList = new CustomList<Customer>();
}

The ServiceMethod() implementation is as follows:

public CustomList<Person> ServiceMethod()
{
return new CustomList<Person>();
}

public class CustomList<T> : Collection<T> where T : class
{
public CustomList()
: base(new List<T>())
{ }
}

Callstack
System.NullReferenceException: Object reference not set to an instance of an object.
at TypeMock.MockManager.GetMocks(Type type)
at TypeMock.MockManager.c(Object A_0)
at dl.c(Object A_0)
at dl.a(String A_0, Object A_1, Object A_2, ref Type A_3, String A_4)
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4)
at TypeMock.InternalMockManager.isMocked(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
at Garaio.Products.RoomsPro.Core.Entities.Helpers.FilterList`1..ctor() in FilterList.cs: line 24
at Garaio.Products.RoomsPro.Web.Controllers.PersonControllerFixture.Test() in PersonControllerFixture.cs: line 73

Am I doing something wrong? Any workarounds?

Studix
asked by studix (680 points)

8 Answers

0 votes
I thought I had a work around, but alas, no luck: but for the Typemock folks here's a lit more of a verbose set of code to verify the bug with if it helps:

Code under test:
    public class PersonService
    {
        public CustomList<Person> ServiceMethod()
        {
            return new CustomList<Person>();
        }
    }

    public class Person { }

    public class Customer { }

    public class CustomList<T> : System.Collections.ObjectModel.Collection<T> where T : class
    {
        public CustomList()
            : base(new System.Collections.Generic.List<T>())
        { }
    } 



Test method:
        [Isolated(), TestMethod()]
        public void Test()
        {
            PersonService service = new PersonService();
            CustomList<Person> list = service.ServiceMethod();

            Assert.IsNotNull(list);

            PersonService personService = Isolate.Fake.Instance<PersonService>();

            Isolate.WhenCalled(() => personService.ServiceMethod()).WillReturn(null);

            CustomList<Person> test = personService.ServiceMethod();
            Assert.IsNull(test);

            //Check that getting a custom list for person is okay.
            CustomList<Person> personList = new CustomList<Person>();
            Assert.IsNotNull(personList);

            // NullReferenceException - Isolator shouldn't be involved in this
            CustomList<Customer> filterList = new CustomList<Customer>();
        }
answered by boo (21.8k points)
0 votes
studix,

I can confirm (using boo's reproduction, thanks!) that this is a bug. I will work to resolve it and send you a patch.

Doron
answered by doron (16.5k points)
0 votes
Thanks Doron

We are using Typemock 5.1.1 and we are not planning to update to the recent version. Is it possible to receive a patch for this version of Typemock?

Cheers
Studix
answered by studix (680 points)
0 votes
Is there a reason you're not updating?
It's always advisable to upgrade for the latest version.

We'll let you know when we completed the patch.

Thanks for your patience
answered by gilz (14.5k points)
0 votes
I can't speak for anyone else, but company policies in most places I've worked are often the reason upgrades don't occur.

Team A is on version 2.4 and believe (for whatever reason) that an upgrade is going to delay their project or cause problems. This reason may be valid or may not be.

Team B wants to go to version 2.5, but the people on team A have more visibility and management decides that whomever on Team A is making resistance about upgrading is the horse to bet on and Team B will just have to live with version 2.4 until Team A okays it.
answered by boo (21.8k points)
0 votes
Brian,

With the upcoming side-by-side feature we're developing, we're going to solve some of the problem.

We'll keep you posted when that's out.
answered by gilz (14.5k points)
0 votes
What boo mentioned in his post is exactly the reason why we do not upgrade.

The side-by-side feature would help us in many ways.

Studix
answered by studix (680 points)
0 votes
Nice to know things are the same all over. Well actually, it's quite depressing.
answered by boo (21.8k points)
...