public async Task AddDiscountAsync__An_unexpected_internal_error_occurred__Should_throw_Exception() { _discountDbServiceMock.Setup(x => x.AddAsync(It.IsAny <Discount>())).ThrowsAsync(new Exception()); _discountDbServiceMock.Setup(x => x.RestrictedAddAsync(It.IsAny <Discount>())).ThrowsAsync(new Exception()); var controller = new DiscountsController(_discountDbServiceMock.Object, _logger, _mapperMock.Object); var discount = new DiscountDto { Id = "1", Description = "test" }; Func <Task> result = async() => await controller.AddDiscountAsync(discount); await result.Should().ThrowExactlyAsync <Exception>(); }
public async Task AddDiscountAsync__Already_there_is_the_same_element_in_database__Should_return_400BadRequest_response() { var discountDto = CreateDiscountDto(_discounts[0]); _mapperMock.Setup(x => x.Map <Discount>(discountDto)).Returns(_discounts[0]); _discountDbServiceMock.Setup(x => x.AddAsync(It.IsNotNull <Discount>())).ThrowsAsync(new InvalidOperationException()); _discountDbServiceMock.Setup(x => x.RestrictedAddAsync(It.IsNotNull <Discount>())).ThrowsAsync(new InvalidOperationException()); var controller = new DiscountsController(_discountDbServiceMock.Object, _logger, _mapperMock.Object); var result = await controller.AddDiscountAsync(discountDto); (result as ObjectResult).StatusCode.Should().Be(400); ((result as ObjectResult).Value as ResponseWrapper).Error.Should().NotBeNull(); }
public async Task AddDiscountAsync__An_internal_error_reffered_to_the_database_occurred__Should_throw_InternalDbServiceException() { // Example of these errors: database does not exist, table does not exist etc. _discountDbServiceMock.Setup(x => x.AddAsync(It.IsAny <Discount>())).ThrowsAsync(new InternalDbServiceException()); _discountDbServiceMock.Setup(x => x.RestrictedAddAsync(It.IsAny <Discount>())).ThrowsAsync(new InternalDbServiceException()); var discount = new DiscountDto { Id = "1", Description = "test" }; var controller = new DiscountsController(_discountDbServiceMock.Object, _logger, _mapperMock.Object); Func <Task> result = async() => await controller.AddDiscountAsync(discount); await result.Should().ThrowExactlyAsync <InternalDbServiceException>(); }
public async Task AddDiscountAsync__Add_succeeded__Should_return_201Created_response_with_added_element() { var validDiscount = new Discount { Id = "12312321321321", Description = "Valid description" }; var validDiscountDto = CreateDiscountDto(validDiscount); _mapperMock.Setup(x => x.Map <Discount>(It.IsNotNull <DiscountDto>())).Returns(validDiscount); _mapperMock.Setup(x => x.Map <DiscountDto>(It.IsNotNull <Discount>())).Returns(validDiscountDto); _discountDbServiceMock.Setup(x => x.AddAsync(validDiscount)).ReturnsAsync(validDiscount); _discountDbServiceMock.Setup(x => x.RestrictedAddAsync(validDiscount)).ReturnsAsync(validDiscount); var controller = new DiscountsController(_discountDbServiceMock.Object, _logger, _mapperMock.Object); var result = await controller.AddDiscountAsync(validDiscountDto); (result as ObjectResult).StatusCode.Should().Be(201); ((result as ObjectResult).Value as ResponseWrapper).Error.Should().BeEquivalentTo(new ApiError()); }