From 249855999cdfb63abcfe1630ef2b2dbba8e103ba Mon Sep 17 00:00:00 2001 From: SaiSDET <166228345+SaiSDET@users.noreply.github.com> Date: Thu, 10 Oct 2024 23:43:48 +0100 Subject: [PATCH] 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 Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> --- .../IntegrationTests/Helpers/TestWebApplicationFactory.cs | 6 ++++++ .../WebMinRouteGroup/Services/TodoService.cs | 6 ++++-- .../MinApiTestsSample/WebMinRouteGroup/appsettings.json | 3 ++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/fundamentals/minimal-apis/samples/MinApiTestsSample/IntegrationTests/Helpers/TestWebApplicationFactory.cs b/fundamentals/minimal-apis/samples/MinApiTestsSample/IntegrationTests/Helpers/TestWebApplicationFactory.cs index d8853e9..577fd91 100644 --- a/fundamentals/minimal-apis/samples/MinApiTestsSample/IntegrationTests/Helpers/TestWebApplicationFactory.cs +++ b/fundamentals/minimal-apis/samples/MinApiTestsSample/IntegrationTests/Helpers/TestWebApplicationFactory.cs @@ -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 { protected override IHost CreateHost(IHostBuilder builder) { + builder.ConfigureHostConfiguration(config => + { + config.AddInMemoryCollection(new Dictionary { { "EmailAddress", "test1@Contoso.com" } }); + }); + builder.ConfigureServices(services => { var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions)); diff --git a/fundamentals/minimal-apis/samples/MinApiTestsSample/WebMinRouteGroup/Services/TodoService.cs b/fundamentals/minimal-apis/samples/MinApiTestsSample/WebMinRouteGroup/Services/TodoService.cs index 54689cd..f7a4f65 100644 --- a/fundamentals/minimal-apis/samples/MinApiTestsSample/WebMinRouteGroup/Services/TodoService.cs +++ b/fundamentals/minimal-apis/samples/MinApiTestsSample/WebMinRouteGroup/Services/TodoService.cs @@ -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 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) diff --git a/fundamentals/minimal-apis/samples/MinApiTestsSample/WebMinRouteGroup/appsettings.json b/fundamentals/minimal-apis/samples/MinApiTestsSample/WebMinRouteGroup/appsettings.json index 10f68b8..9d91f8b 100644 --- a/fundamentals/minimal-apis/samples/MinApiTestsSample/WebMinRouteGroup/appsettings.json +++ b/fundamentals/minimal-apis/samples/MinApiTestsSample/WebMinRouteGroup/appsettings.json @@ -5,5 +5,6 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "EmailAddress": "test1@Contoso.com" }