public void If_Services_Argument_Is_Null() { // Arrange var builder = new CachedOptionsBuilder(); // Act, Assert Assert.Throws <ArgumentNullException>( () => builder.Build(null)); }
public void If_No_Cached_Services_Are_Configured() { // Arrange var builder = new CachedOptionsBuilder(); // Act, Assert Assert.Throws <InvalidOperationException>( () => builder.Build(new Mock <IServiceCollection>().Object)); }
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>()); }
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); }
/// <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); }