AutoMocking Container?

Apr 29, 2008 at 7:45 PM
Is there a way to have Unity create a mock or stub if it cannot resolve a type? I've been looking at container extensions, strategies and policies but the documentation is thin and I'm not sure where to start.


Thanks in advance.
Apr 29, 2008 at 9:12 PM
Roy Osherove did an automocking container here.

This isn't functionality that's built in, but it can be added on without too much trouble.
Jun 8, 2008 at 2:34 PM
Edited Jun 8, 2008 at 2:36 PM

IMHO, Roy just made a nice "helper method" which simplifies creating of mock and it adding to container using Unity build up functionality

If we would check out the Elutian Windsor implementation http://blog.eleutian.com/2007/02/23/TestsAutoMockingIoCContainer.aspx we would see that there is not explicit dependency service registration before the IoC construction of the class using those dependencies

Their code looks like this

  _mocks = new MockRepository();
  _container = new AutoMockingContainer(_mocks);
  _service = _container.Create<DefaultServiceWeAreTesting>();

As you can see in this example there is NO registration to _container before DefaultServiceWeAreTesting creation

What Roy does is something like this

            MockRepository mocks = new MockRepository();
            AutoMockingUnityContainer container =
                           new AutoMockingUnityContainer(mocks);
            container.WillReturnAStubFor<ILogger>();
            container.WillReturnAStubFor<IEmailer>();

            ...some code between....

     // runer has constructor accepting ILoger
            Runner runner = container.Resolve<Runner>();

            Assert.IsNotNull(runner);

So, before constructing Runner he fills the container with appropraiate stubs/mocks

What Roy did is nice helper, but what I would like to see on Unity contrib site is AutoMockingContainer similar to Elutian one where the AMC container would resolve missing service mapping with automatic mock creating

Same Roys test would look like this then

            MockRepository mocks = new MockRepository();
            AutoMockingUnityContainer container =
                           new AutoMockingUnityContainer(mocks);

            ...some code between....

     // runer has constructor accepting ILoger
            Runner runner = container.Resolve<Runner>();

            Assert.IsNotNull(runner);

and container would internaly (once found that there is no ILogger and IEmailer mapping do the

            container.WillReturnAStubFor<ILogger>();
            container.WillReturnAStubFor<IEmailer>();

Thanks,
Nikola Malovic
http://blog.vuscode.com

Apr 8, 2009 at 11:34 AM
Edited Apr 8, 2009 at 11:42 AM
I totally agree with malovicn, Roy's auto mocking container is more of a helper, you still have to do alot of the leg work that AMCs for other IoC frameworks do for you. - I look forward to someone brighter than me writing one.
Apr 8, 2009 at 12:41 PM
I found a "poor man" way for myself of getting "Unity AMC" which I ilustrate in my blog post http://blog.vuscode.com/malovicn/archive/2009/04/04/microsoft-unity-auto-mocking-container.aspx

It is not production ready sample (in order to keep post simple) but it worked well for me in past months as "Unity AMC"
 
Not very impressive (as I said in blog post) but it works for me :)