Auto-mocking container extension

Jun 27, 2009 at 8:31 AM

I wrote a container extension that auto generates mocks for any interfaces that aren't explicitly registered. I use it for reducing the amount of setup required in test code. You can use it like this:

[SetUp]
public void SetUp()
{
container = new UnityContainer();
container.AddNewExtension<AutoMockingContainerExtension>();
}

[Test]
public void Should_be_really_easy_to_test()
{
container.RegisterMock<IDependencyThatNeedsExplicitMocking>()
.Expect(d => d.MyMethod(It.IsAny<int>()))
.Returns("I want to specify the return value");

var service = container.Resolve<ServiceWithThreeDependencies>();
var result = service.DoSomething();

Assert.AreEqual("Look mom, I didn't have to mock the other 2 dependencies!", result);
}

There is full post with source code here http://www.agileatwork.com/auto-mocking-unity-container-extension/