diff --git a/aspnetcore/test/integration-tests.md b/aspnetcore/test/integration-tests.md index d2d0af69cd..e6db729a23 100644 --- a/aspnetcore/test/integration-tests.md +++ b/aspnetcore/test/integration-tests.md @@ -5,7 +5,7 @@ description: Learn how integration tests ensure that an app's components functio monikerRange: '>= aspnetcore-2.1' ms.author: riande ms.custom: mvc -ms.date: 10/07/2019 +ms.date: 10/14/2019 uid: test/integration-tests --- # Integration tests in ASP.NET Core @@ -163,6 +163,23 @@ Web host configuration can be created independently of the test classes by inher Database seeding in the [sample app](https://github.com/aspnet/AspNetCore.Docs/tree/master/aspnetcore/test/integration-tests/samples) is performed by the `InitializeDbForTests` method. The method is described in the [Integration tests sample: Test app organization](#test-app-organization) section. + The SUT's database context is registered in its `Startup.ConfigureServices` method. The test app's `builder.ConfigureServices` callback is executed *after* the app's `Startup.ConfigureServices` code is executed. To use a different database for the tests than the app's database, the app's database context must be replaced in `builder.ConfigureServices`. + + The sample app finds the service descriptor for the database context and uses the descriptor to remove the service registration. Next, the factory adds a new `ApplicationDbContext` that uses an in-memory database for the tests. + + To connect to a different database than the in-memory database, change the `UseInMemoryDatabase` call to connect the context to a different database. To use a SQL Server test database: + + * Reference the [Microsoft.EntityFrameworkCore.SqlServer]https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.SqlServer/) NuGet package in the project file. + * Call `UseSqlServer` with a connection string to the database. + + ```csharp + services.AddDbContext((options, context) => + { + context.UseSqlServer( + Configuration.GetConnectionString("TestingDbConnectionString")); + }); + ``` + 2. Use the custom `CustomWebApplicationFactory` in test classes. The following example uses the factory in the `IndexPageTests` class: [!code-csharp[](integration-tests/samples/3.x/IntegrationTestsSample/tests/RazorPagesProject.Tests/IntegrationTests/IndexPageTests.cs?name=snippet1)] @@ -344,6 +361,8 @@ The sample app seeds the database with three messages in *Utilities.cs* that tes [!code-csharp[](integration-tests/samples/3.x/IntegrationTestsSample/tests/RazorPagesProject.Tests/Helpers/Utilities.cs?name=snippet1)] +The SUT's database context is registered in its `Startup.ConfigureServices` method. The test app's `builder.ConfigureServices` callback is executed *after* the app's `Startup.ConfigureServices` code is executed. To use a different database for the tests, the app's database context must be replaced in `builder.ConfigureServices`. For more information, see the [Customize WebApplicationFactory](#customize-webapplicationfactory) section. + ::: moniker-end ::: moniker range="< aspnetcore-3.0" diff --git a/aspnetcore/test/integration-tests/samples/3.x/IntegrationTestsSample/tests/RazorPagesProject.Tests/CustomWebApplicationFactory.cs b/aspnetcore/test/integration-tests/samples/3.x/IntegrationTestsSample/tests/RazorPagesProject.Tests/CustomWebApplicationFactory.cs index c7382a99c4..d4ec5abeb8 100644 --- a/aspnetcore/test/integration-tests/samples/3.x/IntegrationTestsSample/tests/RazorPagesProject.Tests/CustomWebApplicationFactory.cs +++ b/aspnetcore/test/integration-tests/samples/3.x/IntegrationTestsSample/tests/RazorPagesProject.Tests/CustomWebApplicationFactory.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.EntityFrameworkCore; @@ -16,17 +17,20 @@ namespace RazorPagesProject.Tests { builder.ConfigureServices(services => { - // Create a new service provider. - var serviceProvider = new ServiceCollection() - .AddEntityFrameworkInMemoryDatabase() - .BuildServiceProvider(); + // Remove the app's ApplicationDbContext registration. + var descriptor = services.SingleOrDefault( + d => d.ServiceType == + typeof(DbContextOptions)); - // Add a database context (ApplicationDbContext) using an in-memory - // database for testing. + if (descriptor != null) + { + services.Remove(descriptor); + } + + // Add ApplicationDbContext using an in-memory database for testing. services.AddDbContext((options, context) => { - context.UseInMemoryDatabase("InMemoryDbForTesting") - .UseInternalServiceProvider(serviceProvider); + context.UseInMemoryDatabase("InMemoryDbForTesting"); }); // Build the service provider.