コード例 #1
0
        public void RegisterTest()
        {
            // Given that the LoggingBehavior class has a dependency
            // on an ILogger<> implementation, we could build the
            // service provider and verify that the registration
            // was added, but that would enforce an order of events
            // on service registration.  Skip for now.
            var serviceCollection = new ServiceCollection();

            DependencyRegistrarSupport.AddScopedLogger(serviceCollection);
            DependencyRegistrar.Register(serviceCollection);
            var serviceProvider = serviceCollection.BuildServiceProvider();

            DependencyRegistrarSupport.AssertServiceIsInstanceOfType <ILogger <LoggingBehavior>, FakeLogger <LoggingBehavior> >(serviceProvider);
            DependencyRegistrarSupport.AssertServiceIsInstanceOfType <IPipelineBehavior, LoggingBehavior>(serviceProvider);
        }
コード例 #2
0
        public void RegisterDependencies()
        {
            var configurationRootFromJson = new ConfigurationBuilder().AddJsonFile(@"Configuration\appSettings.test.json").Build();

            var serviceCollection = new ServiceCollection();

            DependencyRegistrarSupport.AddScopedLogger(serviceCollection);
            serviceCollection.RegisterDependencies(configurationRootFromJson);
            var serviceProvider = serviceCollection.BuildServiceProvider();

            // No clean way of getting the count of ILogger<out TCategoryName>
            // without changing the class implemented for the logger
            // from internal to public, which defeats the purpose.
            // Long term solution was to create a Facade for the ILogger,
            // but that's not worth the time, effort, maintenance, and is
            // not a critical feature here that must be tested.

            // In order to verify the internals were actually registered properly
            // we could make them public, but that would defeat the purpose.

            // We could also create verify methods in the dependent Test projects
            // and the references here to call them, but that would mean adding
            // dependencies on each test project.  Again, that would defeat the
            // purpose of doing the tests.

            // Best solution to honor the dependencies while still testing,
            // would be to change ServiceCollectionExtensions to a concrete
            // class that is registered in the Program so it could be
            // injected in the Startup.

            // As a rule of thumb, unit test the critical logic, anything
            // discovered as a bug to verify the issue is fixed.
            // If the infrastructure is critical then, unit test it.
            // If the rules are critical, then unit test the rules.

            // Remember to perform integration tests, manual and automated
            // to ensure your app works as expected and is hooked up properly.

            AssertDependenciesForPipeline(serviceProvider);
            AssertDependenciesForMiddleware(serviceProvider);
            AssertDependenciesForSecurity(serviceProvider);
            AssertDependenciesForRules(serviceProvider);
            AssertDependenciesForTodoItems(serviceProvider);
        }