gpt4 book ai didi

.net - 使用 WebApplicationFactory 将服务替换为模拟服务

转载 作者:行者123 更新时间:2023-12-02 22:31:11 29 4
gpt4 key购买 nike

我正在尝试用 MockMessageFormatter 替换 IMessageFormatter 的实现进行测试。

public class MyMockingWebApplicationFactory<TStartUp> : WebApplicationFactory<Startup>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services => { services.AddTransient<IMessageFormatter, MockMessageFormatter>(); });
}
}

测试看起来像这样:

public class MockingTests : IClassFixture<MyMockingWebApplicationFactory<Startup>>
{
public MockingTests(MyMockingWebApplicationFactory<Startup> webApplicationFactory)
{
_webApplicationFactory = webApplicationFactory;
}

private readonly MyMockingWebApplicationFactory<Startup> _webApplicationFactory;

[Fact]
public async Task MockIt()
{
var client = _webApplicationFactory.WithWebHostBuilder(builder =>
{
//builder.ConfigureServices(services =>
//{
// var foo = ServiceDescriptor.Transient<IMessageFormatter, MessageFormatter>();
// services.Remove(foo);
// services.AddTransient<IMessageFormatter, MockMessageFormatter>();
//});
}).CreateClient();

var message = "Hello";
var expectedReversedMessage = "foo";
var result = await client.GetAsync($"/?message={message}");
var content = await result.Content.ReadAsStreamAsync();
var htmlParser = new HtmlParser();
var htmlDocument = await htmlParser.ParseAsync(content);
var messageElement = htmlDocument.GetElementById("original-message");
var reversedMessageElement = htmlDocument.GetElementById("reversed-message");
Assert.Equal(message, messageElement.InnerHtml);
var reversedMessage = reversedMessageElement.InnerHtml;
Assert.Equal(expectedReversedMessage, reversedMessage);
}
}

我尝试在客户端和 MyMockingWebApplicationFactory 中添加对 ConfigureServices 的调用,但问题似乎是真实 Startup 类在测试注册后执行,因此会覆盖它们。

真正的 StartUp 类是:

public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); ;
services.AddTransient<IMessageFormatter, MessageFormatter>();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDeveloperExceptionPage();
app.UseMvc();
}
}

最佳答案

替换您的代码:

builder.ConfigureServices(services => { services.AddTransient<IMessageFormatter, MockMessageFormatter>(); });

至:

builder.ConfigureTestServices(services =>
{
var serviceProvider = services.BuildServiceProvider();
var descriptor =
new ServiceDescriptor(
typeof(IMessageFormatter),
typeof(MockMessageFormatter),
ServiceLifetime.Transient);
services.Replace(descriptor);
});

关于.net - 使用 WebApplicationFactory 将服务替换为模拟服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53507352/

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