Asp.net core xUnit在调用处理程序(MediatR)时引发System.MissingMethodException
我有一个.NETCoreV2.1。带有CQR的API。为了简化控制器,我使用MediatR。 现在我正试图用xUnit为我的处理程序编写一些单元测试,但它引发了以下异常: StackTrace do Resultado:at Research.Application.EventoContext.Commands.confirmarresencacommandHandler.Handle(confirmarresensacommand 请求,取消令牌(取消令牌)位于 System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& (状态机)在 Research.Application.EventoContext.Commands.confirmarresencacommandHandler.Handle(confirmarresensacommand 请求,取消令牌(取消令牌)位于 Research.Application.EventoContext.Commands.confirmarpresencacomandHandler.Handle() 在里面 C:\Users\public\Desktop\Research\ResearchAPI\app\src\Research.Teste\Commands\EventoCommands.cs:line 38 ---来自引发异常的上一个位置的堆栈结束跟踪---mensage do Resultado:System.MissingMethodException: 找不到方法:“System.Threading.Tasks.TaskAsp.net core xUnit在调用处理程序(MediatR)时引发System.MissingMethodException,asp.net-core,moq,xunit,cqrs,mediatr,Asp.net Core,Moq,Xunit,Cqrs,Mediatr,我有一个.NETCoreV2.1。带有CQR的API。为了简化控制器,我使用MediatR。 现在我正试图用xUnit为我的处理程序编写一些单元测试,但它引发了以下异常: StackTrace do Resultado:at Research.Application.EventoContext.Commands.confirmarresencacommandHandler.Handle(confirmarresensacommand 请求,取消令牌(取消令牌)位于 System.Runtime.
1 MediatR.IMediator.Send(MediatR.IRequest
1,
System.Threading.CancellationToken
这是我的命令和处理程序。因为我没有存储库,所以我使用dbset和SaveChangeAsync()方法创建了一个数据上下文接口:
公共类confirmarresensa命令:IRequest
{
公共Guid id_usuario{get;set;}
公共Guid id_evento{get;set;}
公共列表子事件{get;set;}
}
公共类ConfirmarPresensaCommandHandler:IRequestHandler
{
私有只读IMySqlContext\u context;
专用只读IMediator\u中介;
public ConfirmarPresensaCommandHandler(IMySqlContext上下文,IMediator中介)
{
_上下文=上下文;
_调解人=调解人;
}
公共异步任务句柄(ConfirmarPresensaCommand请求、CancellationToken CancellationToken)
{
var\u presenca=wait\u context.presencasuarios
.Include(p=>p.SubEvento)
.其中(p=>p.codigousario==request.id\u usuario&&
p、 SubEvento.CodigoEvento==request.id\u evento)
.FirstOrDefaultAsync();
如果(_presenca==null)
{
var\u subEvento=await\u context.SubEventos
。其中(s=>s.CodigoEvento==request.id\u evento&&
s、 (校长)
.FirstOrDefaultAsync();
wait_context.presencaasuarios.AddAsync(新presencaasuario
{
codigousario=request.id\u usuario,
CodigoSubEvento=\u subEvento.Codigo,
},取消令牌);
wait_context.saveChangesSync(cancellationToken);
}
return wait_mediator.Send(新的gerarpontuacao命令(request.id_usuario,“Evento”,“Confirmar”);
}
}
这是我的测试。我正在使用Moq.EntityFrameworkCore v2.0.1:
public class ConfirmarEventoTeste : TesteUtils
{
public Mock<IMySqlContext> _moqContext;
public Mock<IMediator> _moqMediator;
public ConfirmarEventoTeste()
{
_moqContext = new Mock<IMySqlContext>();
_moqMediator = new Mock<IMediator>();
_moqMediator.Setup(m => m.Send(It.IsAny<ConfirmarPresencaCommand>(), It.IsAny<CancellationToken>()).ReturnsAsync(It.IsAny<PontuacaoVM>());
}
[Fact]
public async Task ConfirmarPresenca_Sucesso()
{
var _command = new ConfirmarPresensaCommand();
var _handler = new ConfirmarPresensaCommandHandler(_moqContext.Object, _moqMediator.Object);
var _result = await _handler.Handle(_command, new CancellationToken());
Assert.NotNull(_result);
}
}
公共类ConfirmRevenutTeste:TesteUtils
{
公共模拟上下文;
公共模拟媒体;
公开确认
{
_moqContext=新建模拟();
_moqMediator=newmock();
_moqMediator.Setup(m=>m.Send(It.IsAny(),It.IsAny()).ReturnsAsync(It.IsAny());
}
[事实]
公共异步任务ConfirmarPresenca_Sucesso()
{
var_command=new ConfirmarPresensaCommand();
var\u handler=new confirmarpresensacomandHandler(\u moqContext.Object,\u moqMediator.Object);
var _result=await _handler.Handle(_命令,new CancellationToken());
Assert.NotNull(_result);
}
}
我知道我需要为单元测试编写设置,但现在我关心的是,测试上的Handle()调用甚至没有命中我的处理程序
有人能帮我解决这个问题吗?要解决这个问题,你需要在
MediatR
_mediatorMock = new Mock<IMediator>();
_mediatorMock.Setup(x => x.Send(It.IsAny<GerarPontuacaoCommand>(), It.IsAny<CancellationToken>())).Returns(Task.CompletedTask);
使用
在添加所有依赖项后的最后一刻构建服务提供商
从DI获取meditor服务
var mediator=serviceProvider.GetRequiredService();
将您的中介程序
放入处理程序类并执行它
另外,断言
结果
来自其他包的依赖项注入
如果您为DI使用另一个包-您可以在存储库中检查示例并逐个创建自己的测试。它们用于DI。我已经尝试了这两种方法,但仍然没有成功。当我消除内部中介并直接调用处理程序时,它工作正常。但是仍然无法测试控制器,例如,因为MediatR。您是否创建了te DI容器并在控制器中注入MediatR
?
_mediatorMock = new Mock<IMediator>();
_mediatorMock.Setup(x => x.Send(It.IsAny<GerarPontuacaoCommand>(), It.IsAny<CancellationToken>())).Returns(Task.CompletedTask);
var services = new ServiceCollection();
services.AddMediatR(typeof(CustomerLoginRequest).Assembly);
var serviceProvider = services.BuildServiceProvider();
var mediator = serviceProvider.GetRequiredService<IMediator>();