AutoMockingContainer Unity + RhinoMocks + Singleton lifecycle

This is code for minimal no frills automocking container to automatically mocks the dependencies.

References/Credits:

Mocknity: https://github.com/aBaTaPbl4/Mocknity/tree/master/Mocknity (fully flexible custom implementation)

http://www.thomaslevesque.com/2015/06/14/create-an-auto-mocking-container-with-unity-and-fakeiteasy/

https://lostechies.com/joshuaflanagan/2009/02/04/auto-mocking-explained/

https://vuscode.wordpress.com/2009/04/04/microsoft-unity-auto-mocking-container/

 

 

Snippet

public class AutoMockingContainer : UnityContainer
    {
        public AutoMockingContainer()
        {
            AddExtension(new AutoMockExtension(this));
        }
 
       
    }
    class AutoMockExtension : UnityContainerExtension
    {
        AutoMockingContainer container;
        public AutoMockExtension(AutoMockingContainer container)
        {
            this.container = container;
        }
        protected override void Initialize()
        {
            Context.Strategies.Add(new AutoMockingBuilderStrategy(container), Microsoft.Practices.Unity.ObjectBuilder.UnityBuildStage.PreCreation);
 
        }
    }
 
    internal class AutoMockingBuilderStrategy : BuilderStrategy
    {
        private AutoMockingContainer container;
        private readonly Dictionary<Type, object> mocks;
        MockRepository repository;
 
        public AutoMockingBuilderStrategy(AutoMockingContainer container)
        {
            this.container = container;
            mocks = new Dictionary<Type, object>();
            repository = new MockRepository();
        }
 
 
 
        public override void PreBuildUp(IBuilderContext context)
        {
            var type = context.OriginalBuildKey.Type;
            if (container.Registrations.Any(r => r.RegisteredType == type))
            {
                return;
            }
 
            if (type.IsInterface || type.IsAbstract)
            {
                context.Existing = GetOrCreateMock(context, type);
            }
        }
 
        private object GetOrCreateMock(IBuilderContext context, Type t)
        {
            if (mocks.ContainsKey(t))
                return mocks[t];
 
            var mock = repository.DynamicMock(t);
            context.PersistentPolicies.Set<ILifetimePolicy>(new ContainerControlledLifetimeManager(), context.BuildKey);
            context.Existing = mock;
            context.BuildComplete = true;
            return mock;
        }
    }
Advertisements

About funatlearn

Hi i am a software developer working in noida, india. I have created this blog to jolt down my daily learnings at a single place.

Posted on February 13, 2017, in Uncategorized. Bookmark the permalink. Leave a comment.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: