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
        /// <inheritdoc />
        public IAndServicesBuilder WithSetupFor <TService>(Action <TService> scopedServiceSetup)
            where TService : class
        {
            CommonValidator.CheckForNullReference(scopedServiceSetup, nameof(scopedServiceSetup));
            ServiceValidator.ValidateScopedServiceLifetime <TService>(nameof(WithSetupFor));

            scopedServiceSetup(this.HttpContext.RequestServices.GetRequiredService <TService>());

            return(this);
        }
Exemplo n.º 3
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());
        }