Пример #1
0
                public void If_Services_Argument_Is_Null()
                {
                    // Arrange
                    var builder = new CachedOptionsBuilder();

                    // Act, Assert
                    Assert.Throws <ArgumentNullException>(
                        () => builder.Build(null));
                }
Пример #2
0
                public void If_No_Cached_Services_Are_Configured()
                {
                    // Arrange
                    var builder = new CachedOptionsBuilder();

                    // Act, Assert
                    Assert.Throws <InvalidOperationException>(
                        () => builder.Build(new Mock <IServiceCollection>().Object));
                }
Пример #3
0
                public void Expected_Service()
                {
                    // Arrange
                    var services = new ServiceCollection();
                    var builder  = new CachedOptionsBuilder();

                    builder.AddService(new FakeBuilder());

                    // Act
                    builder.Build(services);
                    ServiceProvider provider = services.BuildServiceProvider();

                    // Assert
                    Assert.Equal("cached service", (string)provider.GetService <object>());
                }
Пример #4
0
                public void Without_Removing_PreExisting_Service()
                {
                    // Arrange
                    var services = new ServiceCollection();

                    services.AddScoped <string, string>(provider => "preconditioned");
                    var builder = new CachedOptionsBuilder();

                    builder.AddService(new FakeBuilder());

                    // Act
                    builder.Build(services);

                    // Assert
                    Assert.Equal(2, services.Count);
                }
Пример #5
0
        /// <summary>
        ///     Adds support for caching using Cached.
        /// </summary>
        /// <param name="services">The target service collection.</param>
        /// <param name="options">Options for initializing Cached.</param>
        public static void AddCached(this IServiceCollection services, Action <ICachedOptions> options)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var builder = new CachedOptionsBuilder();

            options?.Invoke(builder);
            builder.Build(services);
        }