Exemplo n.º 1
0
        public SliceFixture()
        {
            var startup  = new Startup(Config);
            var services = new ServiceCollection();

            services.AddLogging();

            var options = new DbContextOptionsBuilder <GuardianDataContext>()
                          .UseInMemoryDatabase(databaseName: DbName)
                          .ConfigureWarnings(x => x.Ignore(InMemoryEventId.TransactionIgnoredWarning))
                          .Options;

            services.AddSingleton(new GuardianDataContext(options));

            startup.ConfigureServices(services);

            var authServiceMock = new Mock <IAuthenticationService>();

            authServiceMock
            .Setup(_ => _.SignInAsync(It.IsAny <HttpContext>(), It.IsAny <string>(), It.IsAny <ClaimsPrincipal>(), It.IsAny <AuthenticationProperties>()))
            .Returns(Task.FromResult((object)null));

            var serviceProviderMock = new Mock <IServiceProvider>();

            serviceProviderMock
            .Setup(_ => _.GetService(typeof(IAuthenticationService)))
            .Returns(authServiceMock.Object);

            var mockHttpContextAccessor = new Mock <IHttpContextAccessor>();
            var context = new DefaultHttpContext()
            {
                // How mock RequestServices?
                RequestServices = serviceProviderMock.Object
            };

            mockHttpContextAccessor.Setup(_ => _.HttpContext).Returns(context);

            services.AddSingleton(mockHttpContextAccessor.Object);
            services.AddSingleton(serviceProviderMock.Object);

            _provider = services.BuildServiceProvider();

            GetDbContext().Database.EnsureCreated();
            _scopeFactory = _provider.GetService <IServiceScopeFactory>();

            var identityHelper = new IdentityHelper(mockHttpContextAccessor.Object);

            context.User = identityHelper.CreateIdentity(GetGlobalTestAccount());
        }