IMediator Mock returns null when I set up it in test(当我在测试中设置IMediator Mock时,它返回空)
问题描述
以下是我的代码:
public sealed class BulkAddStockConditionItemCommandHandler : IRequestHandler<BulkAddStockConditionItemCommand,
UserStockConditionSetsEntity>
{
private readonly IMediator _mediator;
public BulkAddStockConditionItemCommandHandler(IMediator mediator)
{
_mediator = mediator;
}
public async Task<UserStockConditionSetsEntity> Handle(BulkAddStockConditionItemCommand request,
CancellationToken cancellationToken)
{
var conditionalRiskAgreements = new ConditionalRiskAgreementEntity(_userService.CustomerIsin);
var addRes = await _mediator.Send(new AcceptConditionalRiskAgreementCommand(), cancellationToken);
if (addRes == null) throw new Exception();
//...code removed for brevity
}
}
我将IMediator
注入到我的构造函数中,并且在我的单元测试场景中需要addRes
不是NULL
,因此您可以在这里看到我的测试场景:
public async void BulkAddStockConditionItemCommand_when_StockConditionSet_is_null()
{
//arrange
var condition = new ConditionalRiskAgreementEntity(RandomIsin);
var mediator = new Mock<IMediator>();
mediator.Setup(i => i.Send(It.IsAny<ConditionalRiskAgreementEntity>(), It.IsAny<System.Threading.CancellationToken>())).ReturnsAsync(Task.FromResult<object>(condition));
BulkAddStockConditionItemCommand command = new BulkAddStockConditionItemCommand(data);
BulkAddStockConditionItemCommandHandler handler = new BulkAddStockConditionItemCommandHandler(mediator.Object, userservice.Object, repoacc.Object, CacheableRepository.Object);
//Act
var caughtException = await Assert.ThrowsAsync<Exception>(() => handler.Handle(command, new System.Threading.CancellationToken()));
//Assert
Assert.IsType<Exception>(caughtException);
}
我将IMediator
设置为,但是当我运行测试时,它返回null
而不是condition
变量。为什么?
推荐答案
根据显示的代码,测试对象使用AcceptConditionalRiskAgreementCommand
//...
var addRes = await _mediator.Send(new AcceptConditionalRiskAgreementCommand(), cancellationToken);
//...
但在测试中,模拟调解器设置为预期ConditionalRiskAgreementEntity
//...
mediator.Setup(i => i.Send(It.IsAny<ConditionalRiskAgreementEntity>(), It.IsAny<System.Threading.CancellationToken>()))
.ReturnsAsync(Task.FromResult<object>(condition));
//...
默认情况下,当参数匹配器在被调用的成员中不匹配时,模拟将返回NULL。
不需要在ReturnsAsync
中返回Task.FromResult<object>(condition)
,因为这将包装您在Task
首先,我建议您重构测试以使用async Task
而不是async void
,然后更新模拟以预期测试对象中使用的实际参数类型。
public async Task BulkAddStockConditionItemCommand_when_StockConditionSet_is_null() {
//Arrange
var condition = new ConditionalRiskAgreementEntity(RandomIsin);
var mediator = new Mock<IMediator>();
mediator
.Setup(i => i.Send(It.IsAny<AcceptConditionalRiskAgreementCommand>(), It.IsAny<System.Threading.CancellationToken>()))
.ReturnsAsync(condition);
BulkAddStockConditionItemCommand command = new BulkAddStockConditionItemCommand(data);
BulkAddStockConditionItemCommandHandler handler = new BulkAddStockConditionItemCommandHandler(mediator.Object, userservice.Object, repoacc.Object, CacheableRepository.Object);
//Act
var caughtException = await Assert.ThrowsAsync<Exception>(() => handler.Handle(command, new System.Threading.CancellationToken()));
//Assert
Assert.IsType<Exception>(caughtException);
}
这篇关于当我在测试中设置IMediator Mock时,它返回空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:当我在测试中设置IMediator Mock时,它返回空
基础教程推荐
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 XML 转换为通用列表 2022-01-01