【发布时间】:2021-12-28 07:49:35
【问题描述】:
我正在尝试创建一个(大部分)统一的集成测试集,可以针对从 WebApplicationFactory 创建的内存 API 或我们应用程序的完全部署版本。使用XUnit.DependencyInjection,我计划将HttpClient 注入到我的测试中,它可以指向测试服务器或基于环境变量的真实应用程序。
所以要为测试服务器创建一个客户端,我可以在Startup.cs 中运行以下命令:
WebApplicationFactory<Program> app = new();
HttpClient client = app.CreateClient();
这似乎有效。但是,我完全不知道如何将 HttpClient 的这个实现注入到各个测试类中。
这样的东西,不起作用(这样的重载不存在):
services.AddHttpClient<MyTestClass>(client);
这也不是(注入的客户端由于某种原因将BaseAddress设置为null):
services.AddHttpClient<InMemoryServerSelfTests>(c =>
{
c.BaseAddress = client.BaseAddress;
c.Timeout = client.Timeout;
});
我唯一的另一个想法是创建一个包装两个客户端的新类并注入它,但这看起来很混乱:
public class TestClientWrapper
{
public readonly HttpClient Client;
public TestClientWrapper(InMemoryTestServer server)
{
Client = server.CreateClient();
}
public TestClientWrapper(HttpClient client)
{
Client = client;
}
}
// In Startup.cs
public void ConfigureServices(IServiceCollection services)
{
string targetEndpoint = Environment.GetEnvironmentVariable("targetEndpoint"); // Make this configurable
bool isLocal = string.IsNullOrEmpty(targetEndpoint);
if (isLocal)
{
InMemoryTestServer app = new();
services.AddSingleton(new TestClientWrapper(app));
}
else
{
HttpClient client = new();
services.AddSingleton(new TestClientWrapper(client));
}
}
所以说真的,我有点难过...关于如何实现这一点的任何想法?
【问题讨论】:
标签: c# asp.net dependency-injection dotnet-httpclient