Moqing quick reference (Cheat Sheet)

2 Sep


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
     // use new class when asked
     // use an existing instance that implements IPear 

Setting up void calls

public interface IOrange{  void Peel();

View original post 454 more words


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: