Geeks With Blogs

News
Geekus Con Livus Malcolm Anderson's home for Geeks With Lives

The documentation on NMock is scanty at best, non existent at worst.

 

Add into this mix that I self identify as a Mort.  Someone who is generally more concerned about grabbing the data a customer wants and displaying it the way she wants it, so that I can pick up my pay check and go enjoy spending my money.  Apparently, this combination has left me in the position of trying desperately to find example code to improve my oop skills while at the same time, educating me in exactly how weak my those skills are.

 

To that end here is an example of how to mock a collection of your 3rd party object (and remind me to wax lyrically about just how cool mocks are)

 

Mock Setup

IMock[] folderMock = new IMock[2];

      IMock foldersMock;

           

       folderMock[0] = new DynamicMock(typeof(FooFolder));

      folderMock[1] = new DynamicMock(typeof(FooFolder));

 

      foldersMock = new DynamicMock(typeof(FooFolders));

      foldersMock.ExpectAndReturn("Count", 2);

 

      Queue coll = new Queue(2);

      coll.Clear();

      coll.Enqueue(folderMock[0].MockInstance);

      coll.Enqueue(folderMock[1].MockInstance);

 

      foldersMock.ExpectAndReturn("GetEnumerator", coll.GetEnumerator());

 

Calling Code

       FooFolders myFolders = GetFooFolders( “SomeString” );

      if ( myFolders.Count > 0 )

            foreach( FooFolder folder in myFolders )

            {

                  // Do something with folder

            }

      }

 

Posted on Wednesday, August 3, 2005 11:29 PM Agile Development , Stupid Code Tricks | Back to top


Comments on this post: Getting NMock to do a collection

# re: Getting NMock to do a collection
Requesting Gravatar...
Why would you mock a collection class? In this particular case I'd say drop NMock and roll a static stub for the collection class if it really matters. You probably don't really care about the interaction, just that the collection class in the unit test returns a set of data right?

I really don't think you can use NMock and call yourself a Mort anymore.
Left by Jeremy on Aug 04, 2005 2:54 AM

Your comment:
 (will show your gravatar)


Copyright © Malcolm Anderson | Powered by: GeeksWithBlogs.net