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
0 votes
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.
asked by sellingerd (5.7k points)

9 Answers

0 votes
Hi,
Please tell me what version you are using :?:
I tested it with version 3.1.3 on .NET 1.1 and .NET 2.0 and the test work as expected.
answered by scott (32k points)
0 votes
BTW.
A nice way to write the expectations is:
mo.ExpectAndReturn ("GetEnumerable", ie,1).Args ("test", true, DateTime.Today);

.NET will do all the casting to object[] for you, and this way looks cleaner
answered by scott (32k points)
0 votes
I am using .NET version 1.1. I have tested with with TypeMock version 3.1.3.0 (I was having the problem on a previous verion, so I updated it to this version). I am using Nant version NAnt 0.85 (Build 0.85.1869.0; rc2; 12/02/2005).

I have tried it on two different machines (with basically the same configuration and I continue to have the problem). I had created the simplified unit test to demonstrate the problem and the NANT output I posted is from an execute using the posted code. I am surprised you don't have the same result.
answered by sellingerd (5.7k points)
0 votes
Hi,
Please tell me what versions of TypeMock are installed.
1. Under Control Panel -> Add or Remove Programs
2. In the GAC => C:WindowsAssembly
answered by scott (32k points)
0 votes
Under the control panel the version of typemock is 3.1.3.

in the GAC typemock is shown as verison 3.1.3.0.
answered by sellingerd (5.7k points)
0 votes
Ok,
I have managed to reproduce the problem, Thanks for pointing this out.
I'll see how we can fix it.
answered by scott (32k points)
0 votes
Hi,
This is a bug :twisted: , that 'works' in .NET 2.0.

We can send you a patch fix untill the next patch version is out
or use the following workaround:

public object ReturnValue(object[] parameters, object context) 
{
     return new string[] {"test1", "test2"} as IEnumerable;
}

[Test]
public void TextTypeMockEnumerable () {
     MockManager.Init ();
     MockObject mo = MockManager.MockObject (typeof (ITestEnumerable));
     mo.ExpectAndReturn ("GetEnumerable", new DymanicReturnValue(ReturnValue),1).Args ("test", true, DateTime.Today);
     TestClass tc = new TestClass ();
     tc.TestItems (mo.Object as ITestEnumerable);
     MockManager.Verify ();
}
answered by scott (32k points)
0 votes
Thanks. I don't need the patch. I can work around until the next release comes out.

d.
answered by sellingerd (5.7k points)
0 votes
This is fixed in version 3.5
answered by scott (32k points)
...