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
parent
7608c2c1e3
commit
249855999c
|
@ -1,5 +1,6 @@
|
||||||
using Microsoft.AspNetCore.Mvc.Testing;
|
using Microsoft.AspNetCore.Mvc.Testing;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using WebMinRouteGroup.Data;
|
using WebMinRouteGroup.Data;
|
||||||
|
@ -11,6 +12,11 @@ public class TestWebApplicationFactory<TProgram>
|
||||||
{
|
{
|
||||||
protected override IHost CreateHost(IHostBuilder builder)
|
protected override IHost CreateHost(IHostBuilder builder)
|
||||||
{
|
{
|
||||||
|
builder.ConfigureHostConfiguration(config =>
|
||||||
|
{
|
||||||
|
config.AddInMemoryCollection(new Dictionary<string, string?> { { "EmailAddress", "test1@Contoso.com" } });
|
||||||
|
});
|
||||||
|
|
||||||
builder.ConfigureServices(services =>
|
builder.ConfigureServices(services =>
|
||||||
{
|
{
|
||||||
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<TodoGroupDbContext>));
|
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<TodoGroupDbContext>));
|
||||||
|
|
|
@ -7,11 +7,13 @@ public class TodoService : ITodoService
|
||||||
{
|
{
|
||||||
private readonly TodoGroupDbContext _dbContext;
|
private readonly TodoGroupDbContext _dbContext;
|
||||||
private readonly IEmailService _emailService;
|
private readonly IEmailService _emailService;
|
||||||
|
private readonly IConfiguration _configuration;
|
||||||
|
|
||||||
public TodoService(TodoGroupDbContext dbContext, IEmailService emailService)
|
public TodoService(TodoGroupDbContext dbContext, IEmailService emailService, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
_dbContext = dbContext;
|
_dbContext = dbContext;
|
||||||
_emailService = emailService;
|
_emailService = emailService;
|
||||||
|
_configuration = configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async ValueTask<Todo?> Find(int id)
|
public async ValueTask<Todo?> Find(int id)
|
||||||
|
@ -29,7 +31,7 @@ public class TodoService : ITodoService
|
||||||
await _dbContext.Todos.AddAsync(todo);
|
await _dbContext.Todos.AddAsync(todo);
|
||||||
|
|
||||||
if (await _dbContext.SaveChangesAsync() > 0)
|
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)
|
public async Task Update(Todo todo)
|
||||||
|
|
|
@ -5,5 +5,6 @@
|
||||||
"Microsoft.AspNetCore": "Warning"
|
"Microsoft.AspNetCore": "Warning"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*"
|
"AllowedHosts": "*",
|
||||||
|
"EmailAddress": "test1@Contoso.com"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue