ContextContainer !

    [TestClass]
    public class ContextContainerTests
    {
        public ContextContainerTests()
        {
        }


        [ClassInitialize()]
        public static void Initialize(TestContext testContext) 
        {
            var mock = new Mock<ITypeEnumerator>();
            mock.Setup(i => i.GetTypes())
                .Returns(() =>
                {
                    var types = new List<Type>();

                    types.Add(typeof(MockObject));

                    return types;
                });

            ExportProviders.Default = new ExportProvider(mock.Object);

            var factory = new Mock<IObjectFacotry>();

            factory.Setup(i => i.CreateInstance(typeof(MockObject)))
                .Returns(() =>
                    {
                        return new MockObject();
                    });

            factory.Setup(i => i.CreateInstance(typeof(ContractName)))
                .Returns(() =>
                {
                    return new ContractName();
                });


            ObjectContainer.ObjectFactory = factory.Object;
        }


        [TestInitialize()]
        public void MethodInitialize()
        {
            LifetimeManagers.Default = () => new PerRequestLifetimeManager(new MethodRequestContext());
        }

        #region Mock
        class MethodRequestContext : IRequestContext
        {
            Dictionary<object, object> items = new Dictionary<object, object>();

            #region IRequestContext Members

            public System.Collections.IDictionary Items
            {
                get
                {
                    return items;
                }
            }

            #endregion
        }

        interface IMockObject
        {
            string Name
            {
                get;
                set;
            }

            string Blog
            {
                get;
                set;
            }
        }

        [OrderedExport(typeof(IMockObject),0)]
        class MockObject : IMockObject
        {
            public string Name
            {
                get;
                set;
            }

            public string Blog
            {
                get;
                set;
            }
        }

        [OrderedExport("http://sparrow.codeplex.com",typeof(IMockObject),1)]
        class ContractName : IMockObject
        {
            public string Name
            {
                get;
                set;
            }

            public string Blog
            {
                get;
                set;
            }
        }
        #endregion

        [TestMethod]
        public void should_return_the_original_instance()
        {
            var first = ContextContainer.Current.Resolve<IMockObject>();

            var second = ContextContainer.Current.Resolve<IMockObject>();

            Assert.ReferenceEquals(first, second);
        }

        [TestMethod]
        public void should_return_the_original_instance_on_resolving_a_contract_with_the_same_name()
        {
            var first = ContextContainer.Current.Resolve<IMockObject>("first");
            var second =  ContextContainer.Current.Resolve<IMockObject>("first");

            Assert.ReferenceEquals(first, second);
        }

        [TestMethod]
        public void should_return_a_new_instance_when_resolve_an_contract_with_a_different_name()
        {
            var first = ContextContainer.Current.Resolve<IMockObject>("first");

            var second = ContextContainer.Current.Resolve<IMockObject>("second");

            Assert.IsFalse(first.GetHashCode() == second.GetHashCode());
        }

        [TestMethod]
        public void should_support_to_resolve_an_instance_with_a_contract_name()
        {
            var first = ContextContainer.Current.Resolve<IMockObject>("first", "http://sparrow.codeplex.com");

            var second = ContextContainer.Current.Resolve<IMockObject>("first", "http://sparrow.codeplex.com");

            Assert.ReferenceEquals(first, second);
        }

        [TestMethod]
        public void should_support_to_resolve_an_instance_without_a_name()
        {                    
            var first = ContextContainer.Current.Resolve<IMockObject>();
            var second = ContextContainer.Current.Resolve<IMockObject>();

            Assert.ReferenceEquals(first, second);
        }

        [TestMethod]
        public void should_return_the_registered_instance_on_resolving()
        {
            var excepted = new MockObject
            {
                Name = "dayi",
                Blog = "http://walkingboy.cnblogs.com"
            };

            ContextContainer.Current.RegisterInstance<IMockObject>(excepted);

            var actual = ContextContainer.Current.Resolve<IMockObject>();

            Assert.ReferenceEquals(excepted, actual);
        }

        [TestMethod]
        public void should_return_the_registered_instance_on_resolving_with_a_contract_name()
        {
            var excepted = new MockObject
            {
                Name = "dayi",
                Blog = "http://walkingboy.cnblogs.com"
            };

            ContextContainer.Current.RegisterInstance<IMockObject>("registered",excepted);

            var actual = ContextContainer.Current.Resolve<IMockObject>("registered","http://sparrow.codeplex.com");

            Assert.ReferenceEquals(excepted, actual);
        }

    }

Last edited Dec 31, 2010 at 2:01 PM by cnlangzi, version 2

Comments

No comments yet.