【发布时间】:2022-11-09 05:39:00
【问题描述】:
我有一个 AspNetCore Web 应用程序并编写集成测试以使用WebApplicationFactory(即https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests)在内存中运行服务器
像往常一样,应用程序服务是可配置的,换句话说,我们使用IOptions<> 注入到各种服务中。我想测试不同的配置场景,我会动态定义配置。例如:
public class EmailSenderOptions
{
public string Sender { get; set; }
}
// Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<EmailSenderOptions>(config.GetSection("EmailSender"));
// Test
[TestFixture]
public class EmailSenderTests
{
WebApplicationFactory<MyStartup> SUT = //omitted...
[TestCase("noreply@mycompany.com")]
[TestCase("easy-hookup@hackersite.com")]
public void TestSender(string sender)
{
var client = SUT.CreateClient();
SUT.Configuration.Set("EmailSender:Sender", sender); // <-- how?
await client.GetAsync("/email");
}
}
我知道我可以为 IOptions 创建测试实现,但这会更加困难,尤其是在使用 IOptionsMonitor 的情况下。所以我正在寻找一种方法来覆盖配置价值观运行
【问题讨论】:
标签: asp.net-core integration-testing asp.net-core-configuration