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 am using TypeMock 3.7 with the 2.0 .Net Framework and am receiving the following exception in my unit test:

Test method MyNamespace.MyTestClass.MyTestMethod threw exception: System.NullReferenceException: Object reference not set to an instance of an object..


I was able to simplify my code to the following to reproduce the problem:


  
public class Class2<T>
{
     T _value;

     public T Value
     {
          get { return _value; }
          set { _value = value; }
        }
}

public class Class1<T>
{
      Class2<T> _class2;

       public Class2<T> MyClass2
       {
             get { return _class2; }
             set { _class2 = value; }
        }

 }

[TestMethod]
public void MyTestMethod()
 {

     Class1<int> myClass = new Class1<int>();
     using (RecordExpectations recorder = RecorderManager.StartRecording())
      {
                 //This is the line that throws the exception.
                 recorder.ExpectAndReturn(myClass.MyClass2.Value, 5);
        }
 }


If I change the code so that Class2 is not a generic, I no longer get the exception.

Am I doing something wrong, or is this a bug?
asked by shawn (640 points)

8 Answers

0 votes
Shawn,
Welcome to the forums,
You have found a bug :twisted: , thanks for reporting this and posting the code that reproduces the problem.

We will send you a fix offline.

The problem happens when the returned property is a Generic Type and the generic parameter is defined in the calling Type. TypeMock didn't know how to bind the types.
answered by scott (32k points)
0 votes
Great. Thanks for the quick reply. I'll try the patch when I get a chance.
answered by shawn (640 points)
0 votes
Using typemock 3.7.1 and .Net 2.0 I am experiencing a similar problem when my generic type is a IList<object>. I get a different exception tho... Can you send me the patch as well if you think this is related?



System.IndexOutOfRangeException : Index was outside the bounds of the array.

at TypeMock.RecorderManager.a(Object A_0, Object A_1, MethodInfo A_2)
   at TypeMock.RecorderManager.a(String A_0, MethodBase A_1, MethodInfo A_2, Boolean A_3, Object A_4, Object A_5)
   at TypeMock.RecorderManager.a(String A_0, String A_1, Object A_2, Object[] A_3, MethodBase A_4, Object A_5, StackTrace A_6)
   at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Object[] A_4)
   at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters)
answered by matt.darnall (260 points)
0 votes
Hi Matt
It doesn't look like its the same problem.
Can you post an example code that generates the error?
answered by ohad (35.5k points)
0 votes
Here is a sample that will result in the exception:

    public class Temp1<T>
    {
        private T _result;

        public T Result 
        { 
            get { return _result; } 
        }
    }

    public class Temp2 : Temp1<List<string>>
    {
        
    }

    public class Temp3
    {
        public string DoSomething()
        {
            Temp2 myTemp2 = new Temp2();
            return myTemp2.Result[0];
        }

    }

    [TestFixture]
    public class Temp3Fixture
    {
        [TestFixtureSetUp]
        public void Setup()
        {
            MockManager.Init();
        }
        
        [TestFixtureTearDown]
        public void Teardown()
        {
            MockManager.Verify();
        }

        [Test]
        public void DoSomethingTest()
        {
            List<string> myResult = new List<string>(new string[] { "result1" });
            
            using (RecordExpectations recorder = new RecordExpectations())
            {
                Temp2 myTemp2 = new Temp2();
                recorder.ExpectAndReturn(myTemp2.Result, myResult);   

            }

            Assert.AreEqual("result1", new Temp3().DoSomething());
        }
    }
answered by matt.darnall (260 points)
0 votes
Hi Matt
Thanks for posting the example. You actually found a different bug.
We will fix it and send you the patch.
answered by ohad (35.5k points)
0 votes
Hi Matt
I sent you the patch
Please check it and tell me if it works.
answered by ohad (35.5k points)
0 votes
Hi
The patch is available to all in 4.0.0 release
answered by ohad (35.5k points)
...