Пример #1
0
        public IntegrationTestServerFixture()
        {
            // Prep unit testing variables and WebServer customizations - https://docs.microsoft.com/en-us/aspnet/core/testing/integration-testing
            string unitTestDirectory = Directory.GetCurrentDirectory();
            string pathToContentRoot = Path.GetFullPath(Path.Combine(unitTestDirectory, NavigationPathDirectoryToApi));

            var webHostBuilder = DeckOfCards.WebApi.Program.CreateWebHostBuilder(null);

            // Unit testing customizations/overrides:
            server = webHostBuilder
                     .UseEnvironment("Development")
                     .UseContentRoot(pathToContentRoot)
                     .UseUrls(HostingUri.ToString())
                     .ConfigureAppConfiguration((hostingContext, config) =>
            {
                // Config overrides from any number of sources
                var unitTestingOverrides = new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>(StartupExtensions.ConfigKeyRavenDbDatabase, Guid.NewGuid().ToString()),
                };
                config.AddInMemoryCollection(unitTestingOverrides);
            })
                     //.UseStartup<TestStartup>() // Mediatr + Automapper are using GetType() and AppDomains, which won't work with this convention
                     .Build();

            server.Start();

            // Init client:
            HttpClient             = new HttpClient();
            HttpClient.BaseAddress = HostingUri;
            HttpClient.SetAcceptHeaderToJson();
        }
        public DatabaseOutageServerFixture()
        {
            // Prep unit testing variables and WebServer customizations - https://docs.microsoft.com/en-us/aspnet/core/testing/integration-testing
            string unitTestDirectory = Directory.GetCurrentDirectory();
            string pathToContentRoot = Path.GetFullPath(Path.Combine(unitTestDirectory, NavigationPathDirectoryToApi));

            var webHostBuilder = DeckOfCards.WebApi.Program.CreateWebHostBuilder(null);

            // Unit testing customizations/overrides:
            server = webHostBuilder
                     .UseEnvironment("Development")
                     .UseContentRoot(pathToContentRoot)
                     .UseUrls(HostingUri.ToString())
                     .ConfigureServices(services =>
            {
                // Override DI for scenarios that represent outages and unhandled errors
                var docStoreExceptionMock  = new Mock <IDocumentStore>();
                docStoreExceptionMock.Name = "DB Down Mock.";
                //docStoreExceptionMock.CallBase = true;
                docStoreExceptionMock.Setup(obj => obj.OpenAsyncSession()).Throws(new RavenException("This exception was thrown from a mock to mimic a database outage."));
                services.AddSingleton <IDocumentStore>(docStoreExceptionMock.Object);
            })
                     .Build();

            server.Start();

            // Init client:
            HttpClient             = new HttpClient();
            HttpClient.BaseAddress = HostingUri;
            HttpClient.SetAcceptHeaderToJson();
        }