当我在测试中设置IMediator

当我在测试中设置IMediator

本文介绍了当我在测试中设置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时,它返回空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 06:51