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 mock a complex object w/ a private constuctor, this object also has a lsit of other object, wich also have private constructors.

So the problem I'm trying to solve is to create a mocked object, with a mocked list as a member, and the list having 2 mocked objects in it.


I was wondering is anyone has some example code, or can point me to the area where it explains how to setup an indexable mock list.

Thanks

Bas
asked by Bas Hamer (680 points)

4 Answers

0 votes
Hi,
I hope that I understood.

Here is an example that creates a list having 2 mocked objects in it.
// Get a private Type
ReflectionPermission perm = new 
     ReflectionPermission(PermissionState.Unrestricted);
perm.Demand();
Type type1 = typeof(PublicTypeInSameDll).Assembly
     .GetType("PrivateType",true);

// create mocks
MockObject firstMock = MockManager.MockObject(type1);
MockObject secondMock = MockManager.MockObject(type2);

// create list
ArrayList list = new ArrayList();
list.Add(firstMock.Object);
list.Add(secondMock.Object);


of course I might have missed you problem completly, a code snippet could help.
answered by scott (32k points)
0 votes
It is close, but the part I'm haing trouble w/ is mocking the actual arraylist.

so that in effect I end up w/ an indexable, mocked object.

So that rather than having the :
// create list
ArrayList list = new ArrayList();
list.Add(firstMock.Object);
list.Add(secondMock.Object);

code you have something like

// create list
MockList list = new MockList();
list.ExpectIndexAndReturn(0,firstMock.Object);
list.ExpectIndexAndReturn(1,secondMock.Object);

WARNING: the above code snipit is for illustration of the problem and probably won't compile
answered by Bas Hamer (680 points)
0 votes
ArrayList is a mscorlib type and cannot currently be mocked.
We are working on this.

There could be some workarounds.
1. If you are testing for the array you could:
access the ArrayList (by reflection if needed) and you can test that it contains the correct items.
e.g.
testedClass.CallYourMethod();
ArrayList list = testedClass.GetArrayList();
Assert.AreEqual(2,list.Length);


2. If you want the array to change you can do a small refactoring and have the array creation in another method and mock that method
e.g.
  ArrayList list = CreateArray();
...
private ArrayList CreateArray() 
{
   ArrayList list = new ArrayList();
   list.Add(something);
   ...
}

}

// and in the test
mock.ExpectAndReturn("CreateArray", ourMockedArray);
answered by scott (32k points)
0 votes
ok, thanks for the help.

Bas
answered by Bas Hamer (680 points)
...