コード例 #1
0
        public void AreDependenciesRegistered()
        {
            PushToAzureDevOpsArtifacts task = new PushToAzureDevOpsArtifacts();

            var collection = new ServiceCollection();

            task.ConfigureServices(collection);
            var provider = collection.BuildServiceProvider();

            foreach (var dependency in task.GetExecuteParameterTypes())
            {
                var service = provider.GetRequiredService(dependency);
                service.Should().NotBeNull();
            }

            DependencyInjectionValidation.IsDependencyResolutionCoherent(
                s =>
            {
                task.ConfigureServices(s);
            },
                out string message
                )
            .Should()
            .BeTrue(message);
        }
コード例 #2
0
        public void AreDependenciesRegistered()
        {
            var config     = new ConfigurationBuilder();
            var collection = new ServiceCollection();
            Mock <IWebHostEnvironment> env = new Mock <IWebHostEnvironment>();

            env.SetupGet(e => e.EnvironmentName).Returns(Environments.Development);

            collection.AddSingleton <IConfiguration>(config.Build());
            collection.AddSingleton <Startup>();
            collection.AddSingleton(env.Object);
            collection.AddSingleton(env.As <IHostEnvironment>().Object);

            using ServiceProvider provider = collection.BuildServiceProvider();
            var startup = provider.GetRequiredService <Startup>();

            IEnumerable <Type> controllerTypes = typeof(Startup).Assembly.ExportedTypes
                                                 .Where(t => typeof(ControllerBase).IsAssignableFrom(t));

            DependencyInjectionValidation.IsDependencyResolutionCoherent(
                s =>
            {
                foreach (ServiceDescriptor descriptor in collection)
                {
                    s.Add(descriptor);
                }

                s.AddLogging();
                s.AddOptions();
                startup.ConfigureServices(s);
            },
                out string message,
                additionalScopedTypes: controllerTypes).Should().BeTrue(message);
        }
コード例 #3
0
        public void AreDependenciesRegistered()
        {
            Environment.SetEnvironmentVariable("ENVIRONMENT", Environments.Development);

            var config     = new ConfigurationBuilder();
            var collection = new ServiceCollection();

            // The only scenario we are worried about is when running in the ServiceHost
            ServiceHost.ConfigureDefaultServices(collection);

            collection.AddSingleton <IConfiguration>(config.Build());
            collection.AddSingleton <Startup>();
            using ServiceProvider provider = collection.BuildServiceProvider();
            var startup = provider.GetRequiredService <Startup>();

            IEnumerable <Type> controllerTypes = typeof(Startup).Assembly.ExportedTypes
                                                 .Where(t => typeof(ControllerBase).IsAssignableFrom(t));

            Assert.True(DependencyInjectionValidation.IsDependencyResolutionCoherent(
                            s =>
            {
                foreach (ServiceDescriptor descriptor in collection)
                {
                    s.Add(descriptor);
                }

                startup.ConfigureServices(s);
            },
                            out string message,
                            additionalScopedTypes: controllerTypes),
                        message);
        }
コード例 #4
0
        public void MissingOptional_Pass()
        {
            bool isCoherent = DependencyInjectionValidation.IsDependencyResolutionCoherent(
                s => { s.AddSingleton <NeedsSimpleOptional>(); },
                out string message);

            Assert.True(isCoherent, message);
        }
コード例 #5
0
        public void MissingSome_Fail()
        {
            bool isCoherent = DependencyInjectionValidation.IsDependencyResolutionCoherent(
                s => { s.AddSingleton <NeedsSome>(); },
                out string message);

            isCoherent.Should().BeFalse(message);
            message.Should().Contain(nameof(NeedsSome));
        }
コード例 #6
0
        public void MissingSome_Fail()
        {
            bool isCoherent = DependencyInjectionValidation.IsDependencyResolutionCoherent(
                s => { s.AddSingleton <NeedsSome>(); },
                out string message);

            Assert.False(isCoherent);
            Assert.Contains(nameof(NeedsSome), message);
        }
コード例 #7
0
 public void AreDependenciesRegistered()
 {
     DependencyInjectionValidation.IsDependencyResolutionCoherent(s =>
     {
         Environment.SetEnvironmentVariable("ENVIRONMENT", "XUNIT");
         ServiceHost.ConfigureDefaultServices(s);
         Program.Configure(s);
     },
                                                                  out string message).Should().BeTrue(message);
 }
コード例 #8
0
        public void PresentOptional_Pass()
        {
            bool isCoherent = DependencyInjectionValidation.IsDependencyResolutionCoherent(s =>
            {
                s.AddSingleton <ISimple, Simple>();
                s.AddSingleton <NeedsSimpleOptional>();
            },
                                                                                           out string message);

            isCoherent.Should().BeTrue(message);
        }
コード例 #9
0
        public void SimpleScopedRequirementsMess_Pass()
        {
            bool isCoherent = DependencyInjectionValidation.IsDependencyResolutionCoherent(s =>
            {
                s.AddScoped <ISimple, Simple>();
                s.AddScoped <NeedsSimple>();
            },
                                                                                           out string message);

            isCoherent.Should().BeTrue();
        }
コード例 #10
0
        public void SimpleRequirementsMess_Pass()
        {
            bool isCoherent = DependencyInjectionValidation.IsDependencyResolutionCoherent(s =>
            {
                s.AddSingleton <ISimple, Simple>();
                s.AddSingleton <NeedsSimple>();
            },
                                                                                           out string message);

            Assert.True(isCoherent, message);
        }
コード例 #11
0
        public void InstanceRequirementsMess_Pass()
        {
            bool isCoherent = DependencyInjectionValidation.IsDependencyResolutionCoherent(s =>
            {
                s.AddSingleton <IWithValue>(new WithValue("Test1"));
                s.AddSingleton <NeedsValue>();
            },
                                                                                           out string message);

            Assert.True(isCoherent, message);
        }
コード例 #12
0
        public void ValueSome_Pass()
        {
            bool isCoherent = DependencyInjectionValidation.IsDependencyResolutionCoherent(s =>
            {
                s.AddSingleton <IWithValue>(new WithValue("Test1"));
                s.AddSingleton <NeedsSome>();
            },
                                                                                           out string message);

            isCoherent.Should().BeTrue();
        }
コード例 #13
0
        public void DelegateRequirementsMess_Pass()
        {
            bool isCoherent = DependencyInjectionValidation.IsDependencyResolutionCoherent(s =>
            {
                s.AddSingleton <ISimple, Simple>();
                s.AddSingleton <IWithValue>(p => new WithValue(p.GetRequiredService <ISimple>().SimpleValue));
                s.AddSingleton <NeedsValue>();
            },
                                                                                           out string message);

            Assert.True(isCoherent, message);
        }
コード例 #14
0
        public void MismatchedScopes_Fail()
        {
            bool isCoherent = DependencyInjectionValidation.IsDependencyResolutionCoherent(s =>
            {
                s.AddScoped <ISimple, Simple>();
                s.AddSingleton <NeedsSimple>();
            },
                                                                                           out string message);

            isCoherent.Should().BeFalse();
            message.Should().Contain(nameof(NeedsSimple));
        }
コード例 #15
0
 public void AreDependenciesRegistered()
 {
     Assert.True(DependencyInjectionValidation.IsDependencyResolutionCoherent(s =>
     {
         Environment.SetEnvironmentVariable("ENVIRONMENT", "XUNIT");
         ServiceHost.ConfigureDefaultServices(s);
         Program.Configure(s);
         s.AddScoped <FeedCleanerService>();
     },
                                                                              out string message),
                 message);
 }
コード例 #16
0
        public void MismatchedScopes_Fail()
        {
            bool isCoherent = DependencyInjectionValidation.IsDependencyResolutionCoherent(s =>
            {
                s.AddScoped <ISimple, Simple>();
                s.AddSingleton <NeedsSimple>();
            },
                                                                                           out string message);

            Assert.False(isCoherent);
            Assert.Contains(nameof(NeedsSimple), message);
        }
コード例 #17
0
 public void AreDependenciesRegistered()
 {
     DependencyInjectionValidation.IsDependencyResolutionCoherent(
         s =>
     {
         Environment.SetEnvironmentVariable("ENVIRONMENT", "XUNIT");
         ServiceHost.ConfigureDefaultServices(s);
         Program.Configure(s);
     },
         out string message,
         additionalScopedTypes: new[] { typeof(CoreHealthMonitorService) }
         )
     .Should()
     .BeTrue(message);
 }
コード例 #18
0
        public void AreDependenciesRegistered()
        {
            DependencyInjectionValidation.IsDependencyResolutionCoherent(s =>
            {
                Environment.SetEnvironmentVariable("ENVIRONMENT", "XUNIT");
                ServiceHost.ConfigureDefaultServices(s);
                Program.Configure(s);

                // The "IReliableStateManager" is provided by stateful services
                s.AddSingleton(Mock.Of <IReliableStateManager>());

                s.AddScoped <DependencyUpdater>();
            },
                                                                         out string message).Should().BeTrue(message);
        }
コード例 #19
0
        public void AreDependenciesCoherent()
        {
            bool dependenciesCoherent = DependencyInjectionValidation.IsDependencyResolutionCoherent(
                collection =>
            {
                var program = new Program();
                program.ConfigureServiceCollection(collection);
                collection.AddSingleton <IConfiguration>(new ConfigurationBuilder().Build());
            },
                out string errorMessage,
                typeof(Program).Assembly.GetTypes().Where(t => t.GetCustomAttribute <CommandAttribute>() != null)
                );

            dependenciesCoherent.Should().BeTrue(errorMessage);
        }
コード例 #20
0
 public void AreDependenciesRegistered()
 {
     DependencyInjectionValidation.IsDependencyResolutionCoherent(s =>
     {
         s.AddSingleton <IHostEnvironment>(new HostingEnvironment {
             EnvironmentName =
                 Environments.Development
         });
         s.AddBuildAssetRegistry(options =>
         {
             options.UseInMemoryDatabase("BuildAssetRegistry");
             options.EnableServiceProviderCaching(false);
         });
     },
                                                                  out string message).Should().BeTrue(message);
 }
コード例 #21
0
        public void AreDependenciesRegistered()
        {
            UpdatePublishedVersions task = new UpdatePublishedVersions();

            var collection = new ServiceCollection();

            task.ConfigureServices(collection);
            var provider = collection.BuildServiceProvider();

            DependencyInjectionValidation.IsDependencyResolutionCoherent(
                s =>
            {
                task.ConfigureServices(s);
            },
                out string message,
                additionalSingletonTypes: task.GetExecuteParameterTypes()
                )
            .Should()
            .BeTrue(message);
        }
コード例 #22
0
 public void Empty_Pass()
 {
     Assert.True(DependencyInjectionValidation.IsDependencyResolutionCoherent(s => { }, out string message),
                 message);
 }
コード例 #23
0
 public void Empty_Pass()
 {
     DependencyInjectionValidation.IsDependencyResolutionCoherent(s => { }, out string message).Should().BeTrue();
 }