Follow @RoyOsherove on Twitter

Creating a AutoMockingContainer with Microsoft Unity - Pretty darn simple.

updated: made container more readable

Just created a little Auto Mocking Container built on Microsoft Unity. It was very very simple.

here's a test that uses it. We tell the container to Returna stub for T because we will want whoever calls resolve<T> on that interface to get the same mock the we will configure in the record() block. In the record() block we Resolve the interface that returns a mock, and tell the mock what to return. later, "real" code resolves the "Runner" class, which is dependent on our two mocks.

[Test]

        public void MockTwoDependenciesForAConstructor()

        {

            MockRepository mocks = new MockRepository();

            AutoMockingUnityContainer container =

                           new AutoMockingUnityContainer(mocks);

            container.WillReturnAStubFor<ILogger>();

            container.WillReturnAStubFor<IEmailer>();

 

            using (mocks.Record())

            {   //Tell our mock to return true when this method is called

                container.Resolve<ILogger>().IsLogFileFull();

                LastCall.Return(true);

            }

            //runner class takes both of these mocks in its constructor

            Runner runner = container.Resolve<Runner>();

            Assert.IsNotNull(runner);

        }

and here's the full code for the container. As you can see it is pretty simple. ContainerControlledLifetimeManager makes sure we have one single instance built for that object during the container's lifetime or until the type is re-registered using the container.

     public class AutoMockingUnityContainer : UnityContainer

    {

        private MockRepository mocks;

        public AutoMockingUnityContainer(MockRepository mocks)

        {

            this.mocks = mocks;

        }

 

        public AutoMockingUnityContainer WillReturnAStubFor<T>() where T : class

        {

            return (AutoMockingUnityContainer) this.RegisterType(typeof(T), null, null, new StubFactory(typeof(T), mocks,Lifetime.Singleton));

        }

 

        public AutoMockingUnityContainer WillCreateMockFor<T>() where T : class

        {

            return (AutoMockingUnityContainer) this.RegisterType(typeof(T), null, null, new MockFactory(typeof(T),mocks,Lifetime.Singleton));

        }

 

        public AutoMockingUnityContainer WillCreatePartialMockFor<T>(Lifetime lifetime) where T : class

        {

            return (AutoMockingUnityContainer) this.RegisterType(typeof(T), null, null, new PartialMockFactory(typeof(T), mocks, lifetime));

        }

 

        private abstract class AbstractFactory : ContainerControlledLifetimeManager

        {

            protected Type theType;

            protected Lifetime lifetime;

 

            protected AbstractFactory(Type theType, MockRepository mocks, Lifetime lifetime)

            {

                this.theType = theType;

                this.lifetime = lifetime;

                this.mocks = mocks;

            }

 

            protected MockRepository mocks;

 

            protected abstract object GetTheObject(Type type);

 

 

            public override object GetValue()

            {

                object value = base.GetValue();

                if (lifetime==Lifetime.NewEveryTime || value == null)

                {

                    value = GetTheObject(theType);

                    SetValue(value);

                }

                return value;

            }

        }

 

        class MockFactory: AbstractFactory

        {

            public MockFactory(Type theType, MockRepository mocks, Lifetime lifetime)

                : base(theType, mocks, lifetime)

            {

            }

 

            protected override object GetTheObject(Type type)

            {

                return mocks.CreateMock(type);

            }

        }

 

        class StubFactory: AbstractFactory

        {

            public StubFactory(Type theType, MockRepository mocks, Lifetime lifetime)

                      : base(theType, mocks, lifetime)

            {

            }

 

            protected override object GetTheObject(Type type)

            {

                return mocks.Stub(type);

            }

        }

 

        class PartialMockFactory: AbstractFactory

        {

            public PartialMockFactory(Type theType, MockRepository mocks,

                          Lifetime lifetime) : base(theType, mocks, lifetime)

            {

            }

 

            protected override object GetTheObject(Type type)

            {

                return mocks.PartialMock(theType);

            }

        }

    }

 

    public enum Lifetime

    {

        Singleton,

        NewEveryTime

    }

Injecting Typemock Stubs and Mocks using the StructureMap container in a unit test

Itamar at 1.3 (movie)