Moqing quick reference (Cheat Sheet)

2 Sep

@GilesDMiddleton

This post is just a quick reference for myself, when I’ve spent a while away from TDD and want to remember some of the tricks.

Initial construction

var mockFoo = new Mock<IFoo>(MockBehavior.Strict); 

Accessing the mock object as IFoo

IFoo foo = mockFoo.Object;

Initializing the inversion of control container (StructureMap)

Generally use Configure, not Initialize. Configure is additive, Initialize is not. Here’s a complete test method showing the different ways we typically initialize.

[TestMethod()]public void TestMethod()
{
   var mockOrange = new Mock<IOrange>(MockBehavior.Strict);
   MyDefaultHandlerForPear pearConcretion = new MyDefaultHandlerForPear();
   ObjectFactory.Configure( x=> 
   {
      // use mock object
     x.For<IOrange>().Use(mockOrange.Object); 
     // use new class when asked
     x.For<IApple>().Use<MyDefaultHandlerForApple>();
     // use an existing instance that implements IPear 
     x.For<IPear>().Use(pearConcretion);
    });
}

Setting up void calls

public interface IOrange{  void Peel();
}
[TestMethod()]public 

View original post 454 more words

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: