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
Hi,

I found that mocking a non-existing item in a list will return a null reference exception. Here's the code:

        [Test, VerifyMocks]
        public void TestIndexer()
        {
            List<int> myList = new List<int>();
            using ( RecordExpectations recorder = RecorderManager.StartRecording())
            {
                recorder.ExpectAndReturn(myList[4], 1);
            }

            Assert.AreEqual(1, myList[4]);
        }


A null reference exception will be returned:

    
------ Test started: Assembly: WindowsFormsApplication1.exe ------

TestCase 'WindowsFormsApplication1.TestMock.TestIndexer'
failed: System.ArgumentOutOfRangeException : Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
   at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
   at System.ThrowHelper.ThrowArgumentOutOfRangeException()
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   
   at TypeMock.VerifyMocksAttribute.Execute()
   at TypeMock.DecoratorAttribute.CallDecoratedMethod()
   at TypeMock.ClearMocksAttribute.Execute()
   at TypeMock.MethodDecorator.e()
   at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
   at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)


But I think that it should work, even though the list doesn't have any element.
________
vaporizer review
asked by nsoonhui (59.1k points)

7 Answers

0 votes
Hi,

You are trying to mock an MSCorLib type (LIST<>), which is currently not possible in Isolator.

You have, however, uncovered a small bug :oops: - The message and exception you see, should tell you about this, not just an ArgumentException.

Thanks,
answered by gilz (14.5k points)
0 votes
Hi,

You are trying to mock an MSCorLib type (LIST<>), which is currently not possible in Isolator.

You have, however, uncovered a small bug :oops: - The message and exception you see, should tell you about this, not just an ArgumentException.

Thanks,


Thanks for your reply, but what you said is going to confuse me :?


So you are saying that mocking an MSCorLib type is still not possible in Tyemock, but what about mocking other third party component list type such as FarPoint.Win.Spread.cell? They may or may not derive from List<>, but their behavior is similar to that of List<>.

Also, I need to mock List<> type, any suggestion or workaround? Thanks
________
Dodge Viper specifications
answered by nsoonhui (59.1k points)
0 votes
Hi,

Ok, here's the long explanation.
Basically, today we cannot mock types that are declared in mscorlib. It is not the types, but more of the methods.
Now if you have a type that is derived from a type from mscorlib, and does not override the method its base type, which means the base type implementation is called, we can't mock this call either. If however, the derived type overrides this implementation, we can.

The bound type you created List<MyClass> is still considered an mscorlib implementation. If you try to mock the Count, you'll fail.

So how to deal with List<>. Since you can't mock the basic, what you can do is create a wrapper class of your own for the List, for the specific methods you want to mock. Then you mock the wrapper's method. It's a bit inconvenient, but works.

In the future, we are going to allow this. However, we currenty aware that it's not a single feature. It's adding support for types and methods in batches (for example, the first on the list are DateTime, File and Stream). Once we start doing this, I'll give an update on our progress.

Thanks
answered by gilz (14.5k points)
0 votes
Another way to fake the list is to create a fake List and Swap the list in the code. This actual method of swapping depends on the code design.

:arrow: When the List is returned via a function
Mock the function to return the fake list

:arrow: When the List is a field.
Set the field using ObjectState

:arrow: When the List is created in the code.
Override the creation methods and create a fake list.

If you need, please send an example and we will show the best way to test your code
answered by eli (5.7k points)
0 votes
Hi,

Ok, here's the long explanation.
Basically, today we cannot mock types that are declared in mscorlib. It is not the types, but more of the methods.
Now if you have a type that is derived from a type from mscorlib, and does not override the method its base type, which means the base type implementation is called, we can't mock this call either. If however, the derived type overrides this implementation, we can.

The bound type you created List<MyClass> is still considered an mscorlib implementation. If you try to mock the Count, you'll fail.

So how to deal with List<>. Since you can't mock the basic, what you can do is create a wrapper class of your own for the List, for the specific methods you want to mock. Then you mock the wrapper's method. It's a bit inconvenient, but works.

In the future, we are going to allow this. However, we currenty aware that it's not a single feature. It's adding support for types and methods in batches (for example, the first on the list are DateTime, File and Stream). Once we start doing this, I'll give an update on our progress.

Thanks


Hi gilz, it seems that if I were to implement your suggestion, I would need to change my design code to use the wrapper list class instead of the default list class, is it?
________
Toyota TF109 history
answered by nsoonhui (59.1k points)
0 votes
Another way to fake the list is to create a fake List and Swap the list in the code. This actual method of swapping depends on the code design.

:arrow: When the List is returned via a function
Mock the function to return the fake list

:arrow: When the List is a field.
Set the field using ObjectState

:arrow: When the List is created in the code.
Override the creation methods and create a fake list.

If you need, please send an example and we will show the best way to test your code

Hi eli, I would definitely need an example, thanks
________
Progr?s
answered by nsoonhui (59.1k points)
0 votes
Hi,

You can take a look at Travis Illig's post on faking lists. It gives a great walkthrough on that issue.

Thanks,
answered by gilz (14.5k points)
...