public static TDbContext GetDbContext <TDbContext>(this HttpTestContext httpTestContext)
            where TDbContext : class
        {
            ServiceValidator.ValidateScopedServiceLifetime <TDbContext>(nameof(GetDbContext));

            return(httpTestContext
                   .HttpContext
                   .RequestServices
                   .GetRequiredService <TDbContext>());
        }
Exemplo n.º 2
0
        public static TOptions GetOptions <TOptions>(this HttpTestContext httpTestContext)
            where TOptions : class, new()
        {
            ServiceValidator.ValidateScopedServiceLifetime(typeof(IOptions <>), nameof(GetOptions));

            return(httpTestContext
                   .HttpContext
                   .RequestServices
                   .GetRequiredService <IOptions <TOptions> >()
                   .Value);
        }
        public static TDbContext GetDbContext <TDbContext>(this HttpTestContext httpTestContext)
            where TDbContext : class
        {
            var dbContextServices = httpTestContext
                                    .HttpContext
                                    .RequestServices
                                    .GetServices <TDbContext>()
                                    .ToArray();

            if (!dbContextServices.Any())
            {
                throw new InvalidOperationException($"{typeof(TDbContext).ToFriendlyTypeName()} is not registered in the test service provider.");
            }

            if (dbContextServices.Length > 1)
            {
                throw new InvalidOperationException($"Multiple services of type {typeof(TDbContext).ToFriendlyTypeName()} are registered in the test service provider. You should specify the DbContext class explicitly by calling '.WithData(data => data.WithEntities<TDbContext>(dbContextSetupAction)'.");
            }

            ServiceValidator.ValidateScopedServiceLifetime <TDbContext>(nameof(GetDbContext));

            return(dbContextServices.First());
        }
 internal static IDistributedCacheMock GetDistributedCacheMock(this HttpTestContext testContext)
 => testContext.GetDistributedCache().AsDistributedCacheMock();
 public static IDistributedCache GetDistributedCache(this HttpTestContext testContext)
 => testContext.HttpContext.RequestServices.GetRequiredService <IDistributedCache>();
 internal static IMemoryCacheMock GetMemoryCacheMock(this HttpTestContext testContext)
 => testContext.GetMemoryCache().AsMemoryCacheMock();
 public static IMemoryCache GetMemoryCache(this HttpTestContext testContext)
 => testContext.HttpContext.RequestServices.GetRequiredService <IMemoryCache>();