提问人:D.Dave 提问时间:11/15/2023 更新时间:11/15/2023 访问量:12
AutoFac 无法在我与 mediatr 的集成测试中解析 Microsoft.AspNetCore.Http.ISession
AutoFac cannot resolve Microsoft.AspNetCore.Http.ISession in my integration test with mediatr
问:
我想为通过调解器发送的请求编写集成测试。因此,我需要以某种方式解决所有必要的依赖关系。这是我所做的:
public class GetCheckoutRequestTests
{
[Fact]
public void GetCheckoutRequest ()
{
//Arrange
IMediator mediator = BuildMediator();
GetCheckoutRequest request = new()
{
Id= "1090"
};
//Act
SomeDto dto = mediator.Send(request);
//Assert
}
IMediator BuildMediator() {
//AutoFac
var builder = new ContainerBuilder();
builder.RegisterAssemblyTypes(typeof(IMediator).GetTypeInfo().Assembly).AsImplementedInterfaces();
var mediatrOpenTypes = new[] {
typeof(IRequestHandler<,>)
};
foreach (var mediatrOpenType in mediatrOpenTypes) {
builder
.RegisterAssemblyTypes(typeof(GetCheckoutRequest.Handler).GetTypeInfo().Assembly)
.AsClosedTypesOf(mediatrOpenType)
.AsImplementedInterfaces();
}
builder.Register<ServiceFactory>(ctx => {
var c = ctx.Resolve<IComponentContext>();
return t => c.Resolve(t);
});
RegisterDependencies(builder);
var services = new ServiceCollection();
builder.RegisterType<HttpContextAccessor>().As<IHttpContextAccessor>();
services.AddHttpContextAccessor();
builder.Populate(services);
var container = builder.Build();
var serviceProvider = new AutofacServiceProvider(container);
var mediator = serviceProvider.GetRequiredService<IMediator>();
return mediator;
}
private void RegisterDependencies(ContainerBuilder builder)
{
// Register modules
}
}
我不想使用 因为端点在 dto 上返回一个视图构建,并且因为启动时间太长,所以我考虑只为返回 dto 的中介请求本身编写一个集成测试,但我收到此错误,尽管我认为我注册了WebApplicationFactory
ISession
builder.RegisterType<HttpContextAccessor>().As<IHttpContextAccessor>();
services.AddHttpContextAccessor();
无法解析参数“Microsoft.AspNetCore.Http.ISession 会话”
同样的问题Microsoft.Extensions.Logging.ILogger
答: 暂无答案
评论