Added in-memory configuration for app settings in the WebApplicationFactory (#252)

* - Made email address configurable from the TestWebApplicationFactory using an in-memory collection

* Update fundamentals/minimal-apis/samples/MinApiTestsSample/WebMinRouteGroup/appsettings.json

* Update fundamentals/minimal-apis/samples/MinApiTestsSample/IntegrationTests/Helpers/TestWebApplicationFactory.cs

---------

Co-authored-by: CESC <CESC@DESKTOP-B9RH6PM>
Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com>
main
SaiSDET 2024-10-10 23:43:48 +01:00 committed by GitHub
parent 7608c2c1e3
commit 249855999c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 3 deletions

View File

@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using WebMinRouteGroup.Data;
@ -11,6 +12,11 @@ public class TestWebApplicationFactory<TProgram>
{
protected override IHost CreateHost(IHostBuilder builder)
{
builder.ConfigureHostConfiguration(config =>
{
config.AddInMemoryCollection(new Dictionary<string, string?> { { "EmailAddress", "test1@Contoso.com" } });
});
builder.ConfigureServices(services =>
{
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<TodoGroupDbContext>));

View File

@ -7,11 +7,13 @@ public class TodoService : ITodoService
{
private readonly TodoGroupDbContext _dbContext;
private readonly IEmailService _emailService;
private readonly IConfiguration _configuration;
public TodoService(TodoGroupDbContext dbContext, IEmailService emailService)
public TodoService(TodoGroupDbContext dbContext, IEmailService emailService, IConfiguration configuration)
{
_dbContext = dbContext;
_emailService = emailService;
_configuration = configuration;
}
public async ValueTask<Todo?> Find(int id)
@ -29,7 +31,7 @@ public class TodoService : ITodoService
await _dbContext.Todos.AddAsync(todo);
if (await _dbContext.SaveChangesAsync() > 0)
await _emailService.Send("hello@microsoft.com", $"New todo has been added: {todo.Title}");
await _emailService.Send(_configuration["EmailAddress"]!, $"New todo has been added: {todo.Title}");
}
public async Task Update(Todo todo)

View File

@ -5,5 +5,6 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"EmailAddress": "test1@Contoso.com"
}