gpt4 book ai didi

.net-core - 配置 MassTransit 以使用 WebApplicationFactory 进行测试

转载 作者:行者123 更新时间:2023-12-04 13:00:49 26 4
gpt4 key购买 nike

我有一个 ASP.NET Core Web 应用程序并使用 WebApplicationFactory 进行测试设置测试我的 Controller 操作。我之前使用过 RawRabbit,我很容易模拟 IBusClient并将其作为单例添加到 DI 容器中。内WebApplicationFactory<TStartup>.CreateWebHostBuilder()我调用这个扩展方法来添加我的模拟 IBusClient像这样的例子;

/// <summary>
/// Configures the service bus.
/// </summary>
/// <param name="webHostBuilder">The web host builder.</param>
/// <returns>A web host builder.</returns>
public static IWebHostBuilder ConfigureTestServiceBus(this IWebHostBuilder webHostBuilder)
{
webHostBuilder.ConfigureTestServices(services =>
{
services.AddSingleton<IBusClient, MY_MOCK_INSTANCE>
});

return webHostBuilder;
}

但是现在 RawRabbit 存在一些差距,这让我决定转移到 MassTransit。但是,我想知道是否已经有更好的方法来注册 IBus进入我的容器而不在我的测试中模拟它。不确定是否 InMemoryTestFixture , BusTestFixture , 或 BusTestHarness是我的问题的解决方案。不确定如何一起使用它们以及它们的作用。

顺便说一下,在我的 ASP.NET Core 应用程序中,我有一个可重用的扩展方法设置,如下面的代码,用于在启动时连接到 RabbitMQ。

/// <summary>
/// Adds the service bus.
/// </summary>
/// <param name="services">The services.</param>
/// <param name="configurator">The configurator.</param>
/// <returns>A service collection.</returns>
public static IServiceCollection AddServiceBus(this IServiceCollection services, Action<IServiceCollectionConfigurator> configurator)
{
var rabbitMqConfig = new ConfigurationBuilder()
.AddJsonFile("/app/configs/service-bus.json", optional: false, reloadOnChange: true)
.Build();

// Setup DI for MassTransit.
services.AddMassTransit(x =>
{
configurator(x);

// Get the json configuration and use it to setup connection to RabbitMQ.
var rabbitMQConfig = rabbitMqConfig.GetSection(ServiceBusOptionsKey).Get<RabbitMQOptions>();

// Add bus to the container.
x.AddBus(provider => Bus.Factory.CreateUsingRabbitMq(cfg =>
{
cfg.Host(
new Uri(rabbitMQConfig.Host),
hostConfig =>
{
hostConfig.Username(rabbitMQConfig.Username);
hostConfig.Password(rabbitMQConfig.Password);
hostConfig.Heartbeat(rabbitMQConfig.Heartbeat);
});

cfg.ConfigureEndpoints(provider);

// Add Serilog logging.
cfg.UseSerilog();
}));
});

// Add the hosted service that starts and stops the BusControl.
services.AddSingleton<IMessageDataRepository, EncryptedMessageDataRepository>();
services.AddSingleton<IEndpointNameFormatter, EndpointNameFormatter>();
services.AddSingleton<IBus>(provider => provider.GetRequiredService<IBusControl>());
services.AddSingleton<IHostedService, BusHostedService>();

return services;
}

最佳答案

我最终从我的 WebApplicationFactory 中创建了一个方法;

    public void ConfigureTestServiceBus(Action<IServiceCollectionConfigurator> configurator)
{
this._configurator = configurator;
}

使我能够从派生的集成类构造函数中注册测试处理程序;
    public Intg_GetCustomers(WebApplicationTestFactory<Startup> factory)
: base(factory)
{
factory.ConfigureTestServiceBus(c =>
{
c.AddConsumer<TestGetProductConsumer>();
});
}

当我调用扩展方法来添加 MassTransit 的 InMemory 实例时,将使用此配置器
public static IWebHostBuilder ConfigureTestServiceBus(this IWebHostBuilder webHostBuilder, Action<IServiceCollectionConfigurator> configurator)
{
return webHostBuilder
.ConfigureTestServices(services =>
{
// UseInMemoryServiceBus DI for MassTransit.
services.AddMassTransit(c =>
{
configurator?.Invoke(c);

// Add bus to the container.
c.AddBus(provider =>
{
var control = Bus.Factory.CreateUsingInMemory(cfg =>
{
cfg.ConfigureEndpoints(provider);
});

control.Start();

return control;
});
});

services.AddSingleton<IMessageDataRepository, InMemoryMessageDataRepository>();
});
}

关于.net-core - 配置 MassTransit 以使用 WebApplicationFactory<Startup> 进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57541541/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com