public async Task HandleShouldReturnOk() { // Arrange var mapperMock = new Mock <IMapper>(); mapperMock.Setup(x => x.Map <IEnumerable <BusinessUnitModel> >(It.IsAny <object>())).Returns(Enumerable.Empty <BusinessUnitModel>()); var mapper = mapperMock.Object; var businessUnitReadRepositoryMock = new Mock <IBusinessUnitReadRepository>(); businessUnitReadRepositoryMock.Setup(x => x.ListAsync(It.IsAny <Expression <Func <BusinessUnit, bool> > >(), It.IsAny <int?>(), It.IsAny <int?>())).Returns(Task.FromResult(Enumerable.Empty <BusinessUnit>())); var businessUnitReadRepository = businessUnitReadRepositoryMock.Object; var businessUnitOdataProviderMock = new Mock <IBusinessUnitOdataProvider>(); businessUnitOdataProviderMock.Setup(x => x.GetFilterPredicate(It.IsAny <string>())).Returns((Expression <Func <BusinessUnit, bool> >)null); var businessUnitOdataProvider = businessUnitOdataProviderMock.Object; var query = new ListBusinessUnitQuery(null, null, null); var handler = new ListBusinessUnitQueryHandler(mapper, businessUnitReadRepository, businessUnitOdataProvider); // Act var result = await handler.Handle(query, CancellationToken.None); // Assert result.IsFailure.Should().BeFalse(); result.Should().BeOfType(typeof(Result <BusinessUnitsModel>)); }
public async Task HandleShouldReturnFailWhenDatabaseSpecificErrorOccurs() { // Arrange var mapperMock = new Mock <IMapper>(); mapperMock.Setup(x => x.Map <IEnumerable <BusinessUnitModel> >(It.IsAny <object>())).Returns(Enumerable.Empty <BusinessUnitModel>()); var mapper = mapperMock.Object; var businessUnitReadRepositoryMock = new Mock <IBusinessUnitReadRepository>(); businessUnitReadRepositoryMock.Setup(x => x.ListAsync(It.IsAny <Expression <Func <BusinessUnit, bool> > >(), It.IsAny <int?>(), It.IsAny <int?>())).Throws <SomeDatabaseSpecificException>(); var businessUnitReadRepository = businessUnitReadRepositoryMock.Object; var businessUnitOdataProviderMock = new Mock <IBusinessUnitOdataProvider>(); businessUnitOdataProviderMock.Setup(x => x.GetFilterPredicate(It.IsAny <string>())).Returns((Expression <Func <BusinessUnit, bool> >)null); var businessUnitOdataProvider = businessUnitOdataProviderMock.Object; var query = new ListBusinessUnitQuery(null, null, null); var handler = new ListBusinessUnitQueryHandler(mapper, businessUnitReadRepository, businessUnitOdataProvider); // Act var result = await handler.Handle(query, CancellationToken.None); // Assert result.IsFailure.Should().BeTrue(); result.Failures.Should().OnlyContain(x => x.Message == CustomFailures.ListBusinessUnitFailure); }