首页 > 解决方案 > AspNetCore 集成测试多个 WebApplicationFactory 实例?

问题描述

有谁知道是否可以WebApplicationFactory<TStartop>()在同一个单元测试中托管多个实例?

我已经尝试过,但似乎无法解决这个问题。

IE

_client = WebHost<Startup>.GetFactory().CreateClient();
var baseUri = PathString.FromUriComponent(_client.BaseAddress);
_url = baseUri.Value;

_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
    "Bearer", "Y2E890F4-E9AE-468D-8294-6164C59B099Y");

WebHost只是一个帮助类,它允许我在一行中轻松地构建工厂和客户端。

在幕后,它所做的只是:

new WebApplicationFactory<TStartup>()但还有其他一些事情。

如果我能站起来另一个不同的 Web 服务器实例来测试服务器到服务器的功能,那就 太好了。

有谁知道这是否可能?

标签: c#unit-testingasp.net-coreintegration-testingasp.net-core-2.2

解决方案


WebApplicationFactory与接受的答案状态相反,使用两个实例测试服务器到服务器的功能实际上非常容易:

public class OrderAPIFactory : WebApplicationFactory<Order>
{
    public OrderAPIFactory() { ... }
    protected override void ConfigureWebHost(IWebHostBuilder builder) { ... }
}

public class BasketAPIFactory : WebApplicationFactory<BasketStartup>
{
    public BasketAPIFactory() { ... }
    protected override void ConfigureWebHost(IWebHostBuilder builder) { ... }
}

然后您可以按如下方式实例化自定义工厂:

[Fact] 
public async Task TestName()
{
    var orderFactory = new OrderAPIFactory();
    var basketFactory = new BasketAPIFactory();

    var orderHttpClient = orderFactory.CreateClient();
    var basketHttpClient = basketFactory.CreateClient();

    // you can hit eg an endpoint on either side that triggers server-to-server communication
    var orderResponse = await orderHttpClient.GetAsync("api/orders");
    var basketResponse = await basketHttpClient.GetAsync("api/basket");
}

我也不同意公认的答案,即它必然是糟糕的设计:它有它的用例。我的公司有一个微服务基础架构,它依赖于跨微服务的数据复制,并使用带有集成事件的异步消息队列来确保数据一致性。不用说,消息传递功能起着核心作用,需要进行适当的测试。在这种情况下,此处描述的测试设置非常有用。例如,它允许我们彻底测试在消息发布时关闭的服务如何处理消息:

[Fact] 
public async Task DataConsistencyEvents_DependentServiceIsDown_SynchronisesDataWhenUp()
{
    var orderFactory = new OrderAPIFactory();
    var orderHttpClient = orderFactory.CreateClient();

    // a new order is created which leads to a data consistency event being published,
    // which is to be consumed by the BasketAPI service 
    var order = new Order { ... };
    await orderHttpClient.PostAsync("api/orders", order);

    // we only instantiate the BasketAPI service after the creation of the order
    // to mimic downtime. If all goes well, it will still receive the 
    // message that was delivered to its queue and data consistency is preserved
    var basketFactory = new BasketAPIFactory();
    var basketHttpClient = orderFactory.CreateClient();

    // get the basket with all ordered items included from BasketAPI
    var basketResponse = await basketHttpClient.GetAsync("api/baskets?include=orders");
    // check if the new order is contained in the payload of BasketAPI
    AssertContainsNewOrder(basketResponse, order); 
}

推荐阅读