【问题标题】:IMediator Mock returns null when I set up it in test当我在测试中设置 IMediator Mock 时返回 null
【发布时间】:2022-01-22 21:01:40
【问题描述】:

这是我的代码:

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 变量。为什么?

【问题讨论】:

    标签: c# .net-core moq xunit mediator


    【解决方案1】:

    根据所示代码,被测对象使用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));
    
    //...
    

    当参数匹配器在被调用的成员中不匹配时,mock 将默认返回 null

    无需在ReturnsAsync 中返回Task.FromResult&lt;object&gt;(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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多