- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 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;
}
IBus
进入我的容器而不在我的测试中模拟它。不确定是否
InMemoryTestFixture
,
BusTestFixture
, 或
BusTestHarness
是我的问题的解决方案。不确定如何一起使用它们以及它们的作用。
/// <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>();
});
}
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/
我正在为 .NET Core 3.1 Web API 进行集成测试。我关注了这篇文章Painless Integration Testing with ASP.NET Core Web API .这是
我正在尝试用 MockMessageFormatter 替换 IMessageFormatter 的实现进行测试。 public class MyMockingWebApplicationFactor
我已经配置了一个 xUnit 项目来测试 Identity Server 的实现。我创建了一个 TestStartup继承 Startup 的类并覆盖我的 Identity Server 实现以进行测
有谁知道是否可以在同一单元测试中托管WebApplicationFactory()的多个实例? 我已经尝试过并且似乎无法解决这一问题。 IE _client = WebHost.GetFactory(
我正在使用自定义 WebApplication 工厂 public class CustomWebApplicationFactory : WebApplicationFactory where TS
我正在尝试让我的集成测试针对 protected api 端点进行工作。我的测试调用 IDS 连接/ token 端点并获取有效 token 。当我用它来调用 protected api 时,我总是会
我正在尝试让我的集成测试针对 protected api 端点进行工作。我的测试调用 IDS 连接/ token 端点并获取有效 token 。当我用它来调用 protected api 时,我总是会
我的应用程序设计为独立的 aspnet core webapi 自托管可执行文件。 要启动可执行文件,必须将配置文件路径作为命令行参数传递(例如 MyServer.exe --config "path
我需要替换 WebApplicationFactory 中的上下文。我有 MyDbContext,我想用 SQLite 上下文替换它以进行测试。 替换部分工作正常 .ConfigureServices
我正在为我的应用编写集成测试,它使用 .net5 我使用 WebApplicationFactory 和 IHostBuilder 来设置环境。 自定义夹具 -> public class TestF
在 ASP.NET Core 6 中,默认模板将所有内容从 Sturtup.cs 移动进入 Program.cs , 并在 Program.cs 中使用顶级语句,因此没有更多(可说的)Program类
我有一个 ASP.NET Core Web 应用程序并使用 WebApplicationFactory 进行测试设置测试我的 Controller 操作。我之前使用过 RawRabbit,我很容易模拟
我正在为 ASPNetCore Web API 项目编写集成测试。在阅读它时,我遇到了两个术语,首先是 Microsoft.AspNetCore.Mvc.Testing.WebApplicationF
我正在自定义 WebApplicationFactory 以使用原始应用程序项目中的 Startup、appsettings。 目的是创建指向原始应用程序启动的集成测试。 dbcontext 的 ap
我有一个 ASP.NET Core 项目,其中包含一些简单的 Razor 页面和一个 Web API Controller 。 我正在使用 Clean Architecture作为起点。我重命名了项目
我有两个定义如下的集成测试类。当我在 ApiControllerIT 中运行测试时,所有测试都成功运行。 FoundationControllerIT 也是如此。但是,当我同时运行两者时(通过运行封闭
我有一个 ASP.NET Core 2.2 WebApi 项目,它也使用 EF Core 2.2。该项目通过集成测试进行了测试 WebApplicationFactory . 我尝试将 web api
我想使用 WebApplicationFactory 设置我的测试如详述 Integration tests in ASP.NET Core . 在我的一些测试之前,我需要使用在真正的 Startup
我是一名优秀的程序员,十分优秀!