.net 核心集成测试解析范围服务
Posted
技术标签:
【中文标题】.net 核心集成测试解析范围服务【英文标题】:.net core integration tests resolve scoped services 【发布时间】:2021-02-27 13:35:30 【问题描述】:我的集成测试很简单。我调用创建应用程序 API,然后检查应用程序记录是否正确插入。
这是我的 CustomWebApplicationFactory:
public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<Startup>
public CustomWebApplicationFactory()
Configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile($"appsettings.CustomEnvironments.IntegrationTests.json", optional: true)
.AddEnvironmentVariables()
.Build();
Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(Configuration).CreateLogger();
Log.Information("Starting integration tests");
protected override IHostBuilder CreateHostBuilder() =>
base.CreateHostBuilder()
.UseEnvironment(CustomEnvironments.IntegrationTests)
.UseSerilog();
这是我的 ApplicationControllerTests 类:
public class ApplicationControllerTests : IClassFixture<CustomWebApplicationFactory<Startup>>
private readonly HttpClient _client;
private readonly MyDbContext _db;
private readonly CustomWebApplicationFactory<Startup> _factory;
public ApplicationControllerTests(
CustomWebApplicationFactory<Startup> factory)
_factory = factory;
_client = factory.CreateClient();
_db = factory.Services.GetRequiredService<MyDbContext>();
[Fact]
public async Task CreateApplication_WhenCalled_ShouldReturnSuccess()
var request = new CreateApplicationRequest
Name = "App1"
var response = await _client.PostAsync("/api/v1/Application/CreateApplicationForLive", request.ToJsonStringContent());
response.EnsureSuccess();
var applicationId = await response.Deserialize<Guid>();
var application = _db.Set<Application>().Find(applicationId);
Assert.Equal(request.Name, application.Name);
但是当我尝试解析 MyDbContext 范围服务_db = factory.Services.GetRequiredService<MyDbContext>();
时,我收到以下错误:
'无法从根提供商解析范围服务。'
从 WebApplicationFactory 获取作用域服务的正确方法是什么?我在文档Integration tests in asp.net core
中找不到任何示例【问题讨论】:
【参考方案1】:在 WebApplicationFactory 中解决作用域服务的正确方法是创建作用域:
var scope = factory.Services.GetRequiredService<IServiceScopeFactory>().CreateScope();
_db = scope.ServiceProvider.GetRequiredService<MyDbContext>();
【讨论】:
以上是关于.net 核心集成测试解析范围服务的主要内容,如果未能解决你的问题,请参考以下文章