public async Task HandleShouldReturnOk() { // Arrange var instructionReadRepositoryMock = new Mock <IInstructionReadRepository>(); instructionReadRepositoryMock.Setup(x => x.ListAsync(It.IsAny <Expression <Func <Instruction, bool> > >(), It.IsAny <int?>(), It.IsAny <int?>())).Returns(Task.FromResult(Enumerable.Empty <Instruction>())); var instructionReadRepository = instructionReadRepositoryMock.Object; var mapperMock = new Mock <IMapper>(); mapperMock.Setup(x => x.Map <IEnumerable <InstructionModel> >(It.IsAny <object>())).Returns(Enumerable.Empty <InstructionModel>()); var mapper = mapperMock.Object; var query = new ListInstructionQuery(null, null, null); var handler = new ListInstructionQueryHandler(mapper, instructionReadRepository); // Act var result = await handler.Handle(query, CancellationToken.None); // Assert result.IsFailure.Should().BeFalse(); result.Should().BeOfType(typeof(Result <InstructionsModel>)); }
public async Task HandleShouldReturnFailWhenDatabaseSpecificErrorOccurs() { // Arrange var instructionReadRepositoryMock = new Mock <IInstructionReadRepository>(); instructionReadRepositoryMock.Setup(x => x.ListAsync(It.IsAny <Expression <Func <Instruction, bool> > >(), It.IsAny <int?>(), It.IsAny <int?>())).Throws <SomeDatabaseSpecificException>(); var instructionReadRepository = instructionReadRepositoryMock.Object; var mapperMock = new Mock <IMapper>(); mapperMock.Setup(x => x.Map <IEnumerable <InstructionModel> >(It.IsAny <object>())).Returns(Enumerable.Empty <InstructionModel>()); var mapper = mapperMock.Object; var query = new ListInstructionQuery(null, null, null); var handler = new ListInstructionQueryHandler(mapper, instructionReadRepository); // Act var result = await handler.Handle(query, CancellationToken.None); // Assert result.IsFailure.Should().BeTrue(); result.Failures.Should().OnlyContain(x => x.Message == CustomFailures.ListInstructionFailure); }