I am having a wierd problem testing an interface method that returns an IEnumerable. Basically I wish to mock an interface that has a method that returns and IEnumerable. I want the mocked method to return a real IEnumerable that I create from a simple array. Here is some code.
public interface ITestEnumerable {
IEnumerable GetEnumerable (string arg1, bool arg2, DateTime arg3);
}
public class TestClass {
public void TestItems (ITestEnumerable itestEnumerable) {
int count = 0;
foreach (object obj in itestEnumerable.GetEnumerable ("test", true, DateTime.Today)) {
string s = obj as string;
Assert.IsNotNull (s);
++count;
}
Assert.AreEqual (2, count);
}
}
[Test]
public void TextTypeMockEnumerable () {
MockManager.Init ();
MockObject mo = MockManager.MockObject (typeof (ITestEnumerable));
IEnumerable ie = new string[] {"test1", "test2"} as IEnumerable;
Assert.IsNotNull (ie);
Assert.IsTrue (ie is IEnumerable);
mo.ExpectAndReturn ("GetEnumerable", ie,1).Args (new object[] {"test", true, DateTime.Today});
TestClass tc = new TestClass ();
tc.TestItems (mo.Object as ITestEnumerable);
MockManager.Verify ();
}
If I run this I get the error message:
[nunit2] Failures:
[nunit2] 1) Vigis.Vtp.Test.TestWatcherController.TextTypeMockEnumerable : TypeMock.TypeMockException :
[nunit2] *** No method GetEnumerable in type MockITestEnumerable returns System.String[]
[nunit2] at TypeMock.Mock.a(String A_0, Object A_1, Boolean A_2, Boolean A_3, Int32 A_4, Type[] A_5)
[nunit2] at TypeMock.Mock.ExpectAndReturn(String method, Object ret, Int32 timesToRun, Type[] genericTypes)
[nunit2] at Vigis.Vtp.Test.TestWatcherController.TextTypeMockEnumerable() in c:devGFC4VtpWatcherServiceTestWatcherController.cs:line 146
That is, TypeMock does not find the method because it thinks the return type is a string array and not an IEnumerable. Note, before calling the ExpectAndReturn method I assert that the object that I want to return is not null and it is an IEnumerable.
If I replace the string array with an ArrayList (for example) and pass the ArrayList as an IEnumerable, there is no problem and everything works.
Can someone let me know what is happening here? We often implement methods that return IEnumerable (to avoid that our collections are changed by rogue applications). Is there a standard idiom for creating return IEnumerables for use with TypeMock.
thanks in advance.