Geekus Con Livus

Malcolm Anderson's home for Geeks With Lives
posts - 63, comments - 51, trackbacks - 23

My Links

News

Archives

Post Categories

Getting NMock to do a collection

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

            }

      }

 

Print | posted on Wednesday, August 03, 2005 11:29 PM | Filed Under [ Agile Development Stupid Code Tricks ]

Feedback

Gravatar

# re: Getting NMock to do a collection

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.
8/4/2005 2:54 AM | Jeremy
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 

Powered by: