Я пытаюсь заменить реализацию 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);
}
}
I 'мы пытались добавить вызов к 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();
}
}