I recently started evaluating TypeMock for my company and am having the following problem.
I have a collection class:
public abstract class MyAbstractCollection: IList
{
private ArrayList list = new ArrayList();
public void FillData()
{
//fill collection
//...
}
protected internal ArrayList List
{
get
{
return _list;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return _list.GetEnumerator();
}
//other methods
// ...
}
Then multiple collection classes that inherit off of that
This one holds multiple MyObject's
public class MyCollection: TtgBpoCollection
{
public MyCollection()
{
}
public MyObject AddNew()
{
MyObject obj = new MyObject();
List.Aadd(obj);
return obj;
}
}
public class MyObject
{
private int i = 0;
public MyObject()
{
}
public int Value
{
get
{
return i;
}
set
{
i = value;
}
}
}
Then in my code i'm looping through the collection in my MainClass
private int Sum()
{
MyCollection collection = new MyCollection();
int sum = 0;
collection.FillData();
foreach(MyObject obj in collection)
{
sum = sum + obj.Value;
}
return sum;
}
Now in my nMock test I want to test this function by mocking collection
[Test]
public void N01_TestSum()
{
MainClass main = new MainClass();
MyCollection collection = new MyCollection();
//fill collection with test data
//...
Mock collectionMock = MockManager.Mock(typeof(MyCollection));
collectionMock .ExpectAndReturn("GetEnumerable", CreateEnumerator(collectionMock), null);
int returnedValue = main.Sum();
Assert.AreEqual(5, returnedValue, "Incorrect sum");
}
public IEnumerator CreateEnumerator(MyCollection collection)
{
ArrayList list = new ArrayList();
foreach(MyObject obj in collection)
{
list.Add(obj);
}
return list.GetEnumerator();
}
I always get the error:
MyTest.N01_TestSum : TypeMock.TypeMockException :
*** No method GetEnumerable in type MyCollection returns System.Collections.ArrayList+ArrayListEnumeratorSimple
I've simplified everything out but this gets the main idea across. I apologize for any syntax mistakes that may be in there. I'm running the latest version of TypeMock with nUnit and VS 2003.
Am I missing something or is there another way to mock a collection that is being used in a foreach loop?
Thanks!