我正在尝试将IMessageFormatter
的实现替换为MockMessageFormatter
进行测试。
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);
}
}
我尝试在客户端和ConfigureServices
中添加对MyMockingWebApplicationFactory
的调用,但是问题似乎是 real Startup
类是在测试注册后执行,因此将其覆盖。
real 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();
}
}
答案 0 :(得分:2)
替换您的代码:
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);
});
答案 1 :(得分:1)
老问题,只是在使用 asp.net Core 2.1 和更新版本时更新了当前的最佳实践。使用 asp.net Core 5 测试。
下面代码中的services.AddTransient
将替换容器中现有的 IMessageFormatter
实例。
var client = _webApplicationFatory.WithWebHostBuilder(
builder => builder.ConfigureTestServices(
services => services.AddTransient<IMessageFormatter, MockMessageFormatter>()))
.CreateClient();
如果您需要从容器中检索在测试期间插入的服务:
var factoryWithMockService = _webApplicationFactory.WithWebHostBuilder(
builder => builder.ConfigureTestServices(
services => services.AddTransient<IMessageFormatter, MockMessageFormatter>()));
var client = factoryWithMockService.CreateClient();
var mockService = factoryWithMockService.Services.GetRequiredService<IMessageFormatter>();
使用 Moq 时:
var messageFormatterMock = new Mock<IMessageFormatter>();
messageFormatterMock.Setup(svc => svc...).Returns(...);
var client = _webApplicationFatory.WithWebHostBuilder(
builder => builder.ConfigureTestServices(
services => services.AddTransient(_ => messageFormatterMock.Object)))
.CreateClient();
...
messageFormatterMock.Verify(svc => svc..., ...);