如何从 CustomWebApplicationFactory 获取所有服务?Startup类对IDepartmentRepository和IDepartmentAppService进行了依赖注入。想要获取新的 DI 存储库或服务。
我们正在创建指向原始应用程序启动的集成测试。
namespace Integrationtest
{
public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureAppConfiguration((hostingContext, configurationBuilder) =>
{
var type = typeof(TStartup);
var path = @"C:\\OriginalApplicationWebAPI";
configurationBuilder.AddJsonFile($"{path}\\appsettings.json", optional: true, reloadOnChange: true);
configurationBuilder.AddEnvironmentVariables();
});
}
}
}
public class DepartmentAppServiceTest : IClassFixture<CustomWebApplicationFactory<OriginalApplication.Startup>>
{
public testDbContext context;
private readonly HttpClient _client;
public DepartmentAppServiceTest(CustomWebApplicationFactory<OriginalApplication.Startup> factory)
{
_client = factory.CreateClient();
factory. // trying to find services here for IDepartmentRepository
}
https://fullstackmark.com/post/20/painless-integration-testing-with-aspnet-core-web-api
皈依舞
慕标5832272
相关分类