Daniel Cazzulino's Blog : Automocking container with Moq

Automocking container with Moq

I realized that I never blogged about this cool feature contributed by Slava to Moq-Contrib.

Auto-mocking containers was an idea originally (IIRC) from the guys at Eleutian, later on picked up by Jeremy Miller with Rhino Mocks and StructureMap.

The wiki document Slava put together on Automocking is a fantastic overview. Basically, you can have an (Autofac-powered) container create your objects under test with all its dependencies injected as mocks:

var factory = new MockFactory(MockBehavior.Loose);  
var container = new AutoMockContainer(factory);  
var tree = container.Create<Tree>();

Because it uses a MockFactory, it also integrates with the rest of the features in Moq that are available through the factory, such as consistent settings for mock behavior, whether to call base class implementations, and the default value behavior.

Go ahead and read the wiki and get the bits!

/kzu