forked from dotnet-architecture/eShopOnWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
And updated functional tests to use CustomWebApplicationFactory.
- Loading branch information
Showing
8 changed files
with
87 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 8 additions & 45 deletions
53
tests/FunctionalTests/Web/Controllers/CatalogControllerIndex.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
tests/FunctionalTests/Web/Controllers/CustomWebApplicationFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using Infrastructure.Data; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using Microsoft.eShopWeb; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace FunctionalTests.Web.Controllers | ||
{ | ||
public class CustomWebApplicationFactory<TStartup> | ||
: WebApplicationFactory<Startup> | ||
{ | ||
protected override void ConfigureWebHost(IWebHostBuilder builder) | ||
{ | ||
builder.UseEnvironment("Testing"); | ||
builder.ConfigureServices(services => | ||
{ | ||
// Create a new service provider. | ||
var serviceProvider = new ServiceCollection() | ||
.AddEntityFrameworkInMemoryDatabase() | ||
.BuildServiceProvider(); | ||
|
||
// Add a database context (ApplicationDbContext) using an in-memory | ||
// database for testing. | ||
services.AddDbContext<CatalogContext>(options => | ||
{ | ||
options.UseInMemoryDatabase("InMemoryDbForTesting"); | ||
options.UseInternalServiceProvider(serviceProvider); | ||
}); | ||
|
||
// Build the service provider. | ||
var sp = services.BuildServiceProvider(); | ||
|
||
// Create a scope to obtain a reference to the database | ||
// context (ApplicationDbContext). | ||
using (var scope = sp.CreateScope()) | ||
{ | ||
var scopedServices = scope.ServiceProvider; | ||
var db = scopedServices.GetRequiredService<CatalogContext>(); | ||
var loggerFactory = scopedServices.GetRequiredService<ILoggerFactory>(); | ||
|
||
var logger = scopedServices | ||
.GetRequiredService<ILogger<CustomWebApplicationFactory<TStartup>>>(); | ||
|
||
// Ensure the database is created. | ||
db.Database.EnsureCreated(); | ||
|
||
try | ||
{ | ||
// Seed the database with test data. | ||
CatalogContextSeed.SeedAsync(db, loggerFactory).Wait(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger.LogError(ex, $"An error occurred seeding the " + | ||
"database with test messages. Error: {ex.Message}"); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters