我有一个 dot net core 项目,我正在尝试在我的两个BackgroundServices
.
以下是我目前的xUnit
测试:
[Fact]
public async Task FileWatcherIntegration()
{
IServiceCollection services = new ServiceCollection();
services.AddHostedService<FileWatcherService>();
services.AddHostedService<FailedTradesWorker>();
services.AddSingleton(fileWatcherOptions);
services.AddSingleton<ILogger<FileWatcherService>>(logger);
var serviceProvider = services.BuildServiceProvider();
var fileWatcher = serviceProvider.GetService<IHostedService>() as FileWatcherService;
var tradeWorker = serviceProvider.GetService<IHostedService>() as FailedTradesWorker;
// act
await tradeWorker.StartAsync(CancellationToken.None);
await fileWatcher.StartAsync(CancellationToken.None);
/* Ommited */
await tradeWorker.StopAsync(CancellationToken.None);
await fileWatcher.StopAsync(CancellationToken.None);
// assert
Assert.True(File.Exists(fileWatcherOptions.DestinationPathAndName));
}
我的测试失败var fileWatcher = serviceProvider.GetService<IHostedService>() as FileWatcherService;
并显示错误消息: Unable to resolve service for type
为该测试设置我的服务的正确方法是什么?