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时,它返回空


- C#MongoDB使用Builders查找派生对象 2022-09-04
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01