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'm having trouble mocking indexes with the ExpectGetIndexAlways method. I consistently get a "Cannot mock methods defined in types from mscorlib assembly".

public class SomeCollection : Collection<MyObject>
{ .... }


MyObject obj = new MyObject();
MockObject mo = MockManager.MockObject(typeof(SomeCollection), Constructor.Mocked);
mo.ExpectGetIndexAlways(obj).Args(0); -- Throw exception

I couldn't find any example use of this anywhere, could you post me some sample code please?

Thanks
asked by kerryjohnreynolds (640 points)

3 Answers

0 votes
Hi
Actually the exception message is correct.
Since SomeCollection inherits from Collection class which hides deep in the dark caves of mscorlib assembly we can not mock it :twisted:

Lets take the indexers example from the MSDN:

    class SampleCollection<T>
    {
        private T[] arr = new T[100];
        public T this[int i]
        {
            get
            {
                return arr[i];
            }
            set
            {
                arr[i] = value;
            }
        }
    }

    [TestFixture]
    public class TstClass
    {
        [Test]
        public void Test()
        {
            MyObject obj = new MyObject();
            MockObject mo = MockManager.MockObject(typeof(SampleCollection<MyObject>),Constructor.Mocked);
            mo.ExpectGetIndexAlways(obj).Args(0);
        }
    }


One last thing: mocking mscorlib types is in our features list so you can expect it in one of the future versions of TypeMock.
answered by ohad (35.4k points)
0 votes
That's a shame. The main reason I choose Typemock over others is because I don't have to change my object model to support unit tests, e.g. by having to supply mocks via constructors etc.

I think I'll settle for less isolation in my tests and stick with the Collection base class until you come up with a mscorlib solution.

When will that be by the way? :)
answered by kerryjohnreynolds (640 points)
0 votes
Hi
I can not say when the feature will be ready but it is high in our list. :oops:
I'm sure that we will ring all bells when it's done.
By the way except from the mscorlib issue TypeMock still allows you not to change your object model to support unit tests...
answered by ohad (35.4k points)
...