Ejemplo n.º 1
0
            public void Adds_a_singleton_resolver_with_a_key()
            {
                var normalChain = new ResolverChain();

                new InternalConfiguration(
                    new Mock <ResolverChain>().Object, normalChain,
                    new RootDependencyResolver(),
                    new Mock <AppConfigDependencyResolver>().Object).RegisterSingleton("Bilbo", "Baggins");

                Assert.Equal("Bilbo", normalChain.GetService <string>("Baggins"));
                Assert.Null(normalChain.GetService <string>("Biggins"));
            }
            public void Adds_a_singleton_resolver_with_a_key()
            {
                var normalChain = new ResolverChain();

                new InternalConfiguration(
                    new Mock<ResolverChain>().Object, normalChain,
                    new RootDependencyResolver(),
                    new Mock<AppConfigDependencyResolver>().Object).RegisterSingleton("Bilbo", "Baggins");

                Assert.Equal("Bilbo", normalChain.GetService<string>("Baggins"));
                Assert.Null(normalChain.GetService<string>("Biggins"));
            }
Ejemplo n.º 3
0
            public void Adds_a_singleton_resolver_with_a_key_predicate()
            {
                var normalChain = new ResolverChain();

                new InternalConfiguration(
                    new Mock <ResolverChain>().Object, normalChain,
                    new RootDependencyResolver(),
                    new Mock <AppConfigDependencyResolver>().Object).RegisterSingleton("Bilbo", k => ((string)k).StartsWith("B"));

                Assert.Equal("Bilbo", normalChain.GetService <string>("Baggins"));
                Assert.Equal("Bilbo", normalChain.GetService <string>("Biggins"));
                Assert.Null(normalChain.GetService <string>("More than half a Brandybuck"));
            }
            public void Adds_a_singleton_resolver_with_a_key_predicate()
            {
                var normalChain = new ResolverChain();

                new InternalConfiguration(
                    new Mock<ResolverChain>().Object, normalChain,
                    new RootDependencyResolver(),
                    new Mock<AppConfigDependencyResolver>().Object).RegisterSingleton("Bilbo", k => ((string)k).StartsWith("B"));

                Assert.Equal("Bilbo", normalChain.GetService<string>("Baggins"));
                Assert.Equal("Bilbo", normalChain.GetService<string>("Biggins"));
                Assert.Null(normalChain.GetService<string>("More than half a Brandybuck"));
            }
Ejemplo n.º 5
0
            public void RootResolver_is_added_to_the_non_app_config_resolver_chain()
            {
                var normalChain      = new ResolverChain();
                var mockRootResolver = new Mock <RootDependencyResolver>();

                new InternalConfiguration(new Mock <ResolverChain>().Object, normalChain, mockRootResolver.Object);

                normalChain.GetService <object>("Foo");

                mockRootResolver.Verify(m => m.GetService(typeof(object), "Foo"));
            }
Ejemplo n.º 6
0
        public void GetService_registers_SQL_Server_as_a_fallback_if_it_is_not_already_registered()
        {
            var providerTypeName = string.Format(
                CultureInfo.InvariantCulture,
                "System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer, Version={0}, Culture=neutral, PublicKeyToken=b77a5c561934e089",
                new AssemblyName(typeof(DbContext).Assembly.FullName).Version);

            var mockSqlProvider = new Mock <DbProviderServices>();

            mockSqlProvider.Setup(m => m.GetService(typeof(string), null)).Returns("System.Data.SqlClient");

            var mockSection = CreateMockSectionWithProviders();
            var mockFactory = CreateMockFactory(mockSection.Object);

            mockFactory.Setup(m => m.TryGetInstance(providerTypeName))
            .Returns(mockSqlProvider.Object);

            var appConfig = new AppConfig(new ConnectionStringSettingsCollection(), null, mockSection.Object, mockFactory.Object);

            var mockConfiguration = new Mock <InternalConfiguration>(null, null, null, null);

            var someRandomThing = new Random();

            mockSqlProvider.Setup(m => m.GetService(typeof(Random), null)).Returns(someRandomThing);

            var resolvers = new ResolverChain();

            mockConfiguration.Setup(m => m.AddSecondaryResolver(It.IsAny <IDbDependencyResolver>()))
            .Callback <IDbDependencyResolver>(resolvers.Add);

            new AppConfigDependencyResolver(appConfig, mockConfiguration.Object, mockFactory.Object).GetService <IPilkington>();

            mockConfiguration.Verify(m => m.AddSecondaryResolver(It.IsAny <DbProviderServices>()), Times.Exactly(4));
            mockConfiguration.Verify(m => m.AddSecondaryResolver(It.IsAny <SingletonDependencyResolver <DbProviderServices> >()), Times.Once());

            Assert.Equal("Robot.Rock", resolvers.GetService <string>());
            Assert.Same(someRandomThing, resolvers.GetService <Random>());
            Assert.Same(mockSqlProvider.Object, resolvers.GetService <DbProviderServices>("System.Data.SqlClient"));
        }
Ejemplo n.º 7
0
        public void GetService_returns_null_if_no_resolver_in_the_chain_resolves_the_dependency()
        {
            var mockResolver1 = CreateMockResolver("Steve", new Mock <IPilkington>().Object);
            var mockResolver2 = CreateMockResolver("Ricky", new Mock <IPilkington>().Object);

            var chain = new ResolverChain();

            chain.Add(mockResolver1.Object);
            chain.Add(mockResolver2.Object);

            Assert.Null(chain.GetService <IPilkington>("Karl"));

            mockResolver1.Verify(m => m.GetService(typeof(IPilkington), "Karl"), Times.Once());
            mockResolver2.Verify(m => m.GetService(typeof(IPilkington), "Karl"), Times.Once());
        }
Ejemplo n.º 8
0
        public void GetService_and_Add_can_be_accessed_from_multiple_threads_concurrently()
        {
            for (var i = 0; i < 30; i++)
            {
                var bag      = new ConcurrentBag <IPilkington>();
                var resolver = new ResolverChain();
                var karl     = new Karl();

                ExecuteInParallel(
                    () =>
                {
                    resolver.Add(new SingletonDependencyResolver <IPilkington>(karl, "Karl"));
                    bag.Add(resolver.GetService <IPilkington>("Karl"));
                });

                Assert.Equal(20, bag.Count);
                Assert.True(bag.All(c => karl == c));
            }
        }
Ejemplo n.º 9
0
        public void GetService_returns_the_service_returned_by_the_most_recently_added_resolver_that_resolves_the_dependency()
        {
            var karl = new Mock <IPilkington>().Object;

            var mockResolver1 = CreateMockResolver("Karl", new Mock <IPilkington>().Object);
            var mockResolver2 = CreateMockResolver("Karl", karl);
            var mockResolver3 = CreateMockResolver("Ricky", new Mock <IPilkington>().Object);

            var chain = new ResolverChain();

            chain.Add(mockResolver1.Object);
            chain.Add(mockResolver2.Object);
            chain.Add(mockResolver3.Object);

            Assert.Same(karl, chain.GetService <IPilkington>("Karl"));

            mockResolver1.Verify(m => m.GetService(typeof(IPilkington), "Karl"), Times.Never());
            mockResolver2.Verify(m => m.GetService(typeof(IPilkington), "Karl"), Times.Once());
            mockResolver3.Verify(m => m.GetService(typeof(IPilkington), "Karl"), Times.Once());
        }
 /// <inheritdoc />
 public virtual object GetService(Type type, object key)
 {
     return(_secondaryResolvers.GetService(type, key) ?? _resolvers.GetService(type, key));
 }
Ejemplo n.º 11
0
            public void RootResolver_is_added_to_the_non_app_config_resolver_chain()
            {
                var normalChain = new ResolverChain();
                var mockRootResolver = new Mock<RootDependencyResolver>();

                new DbConfiguration(new Mock<ResolverChain>().Object, normalChain, mockRootResolver.Object);

                normalChain.GetService<object>("Foo");

                mockRootResolver.Verify(m => m.GetService(typeof(object), "Foo"));
            }