Here is my code:
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
}
}
I inject the IMediator into my constructor and I need the addRes not be NULL in my unit test scenario, so here you can see my test scenario:
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);
}
I set up the IMediator as but when I run the test it returns null instead of condition variable. Why?