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 Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!

Problem mocking an index accessor

+1 vote
I'm working with the Autodesk Inventor SDK Com Interop library.

I've got a test set up where I need to return a mocked object from an indexed accessor but it gives me null instead of what I set it up to return:

            var drawing = Isolate.Fake.Instance<DrawingDocument>();
            var sheets = Isolate.Fake.Instance<Sheets>();
            var sheet = Isolate.Fake.Instance<Sheet>();
            Isolate.WhenCalled(() => sheets[null]).WillReturn(sheet);
            var testSheet = drawing.Sheets[1];
            testSheet.Activate();

that last line throws an exception because testSheet is null. What am I doing wrong with this?
asked Jan 2 by ajw1970 (6,140 points)
reshown Jan 28 by leahtypemock

1 Answer

0 votes

Hi,

When faking an indexer you need to pass the index into the `WhenCalled` as well (in this case the index is 1):

Isolate.WhenCalled(() => sheets[1]).WillReturn(sheet);

 

answered Jan 29 by Raphy (3,170 points)
...