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 trying to influence the Count property of a list.

var project = Isolate.Fake.Instance<ProjectDefinition>();

Isolate.WhenCalled(() => project.Questions).WillReturnCollectionValuesOf
  (
    new List<DecisionTree>()
    {
      Isolate.Fake.Instance<DecisionTree>()
    }
  );  
Isolate.WhenCalled(() => project.Questions.Count).WillReturn(3);


But I get an mscorlib can't be mocked exception. Is this correct?

Is there any way to influence the Count property of a collection?

Thanks
asked by dvdstelt (5.3k points)

4 Answers

0 votes
Hi Dennis,

List is indeed in mscorlib, and when you try to set the behavior on that, you get the proper exception.

Maybe you can try this. Instead of return the List, try returning an IList. Interfaces from mscorlib can be mocked, and then you can set the Count property to return 3.

Let me know if it worked.

Thanks
answered by gilz (14.5k points)
0 votes
It might be easier to return a fake collection with 3 items.

var fakeProject = Isolate.Fake.Instance<ProjectDefinition>();

Isolate.WhenCalled(() =>fakeProject.Questions).WillReturnCollectionValuesOf
  (
    new List<DecisionTree>()
    {
      Isolate.Fake.Instance<DecisionTree>(),
      Isolate.Fake.Instance<DecisionTree>(),
      Isolate.Fake.Instance<DecisionTree>()
    }
  );  
answered by eli (5.7k points)
0 votes
It might be easier to return a fake collection with 3 items.


The cool thing is, THAT'S EXACTLY where I get me mscorlib exception!

Test method Tests.Invoicing.ExactGeneratorTests.TestName threw exception: TypeMock.TypeMockException:
*** Cannot mock types from mscorlib assembly..
answered by dvdstelt (5.3k points)
0 votes
True, remove the Fake.Instance line or use CallOriginal

var fakeProject = new ProjectDefinition();

Isolate.WhenCalled(() =>fakeProject.Questions).WillReturnCollectionValuesOf
  (
    new List<DecisionTree>()
    {
      Isolate.Fake.Instance<DecisionTree>(),
      Isolate.Fake.Instance<DecisionTree>(),
      Isolate.Fake.Instance<DecisionTree>()
    }
  ); 
answered by eli (5.7k points)
...