public async Task AddItem_success() { //arrange var customerId = "123"; var basket = GetCustomerBasketFake(customerId); BasketItem input = new BasketItem("004", "004", "produto 004", 45.67m, 4); var items = basket.Items; items.Add(input); _basketRepositoryMock .Setup(c => c.AddBasketAsync(customerId, It.IsAny <BasketItem>())) .ReturnsAsync(new CustomerBasket { CustomerId = customerId, Items = items }) .Verifiable(); var controller = new BasketController( _basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object, _loggerMock.Object, _configurationMock.Object); //act ActionResult <CustomerBasket> actionResult = await controller.AddItem(customerId, input); //assert OkObjectResult okObjectResult = Assert.IsType <OkObjectResult>(actionResult.Result); CustomerBasket customerBasket = Assert.IsAssignableFrom <CustomerBasket>(okObjectResult.Value); Assert.Equal(4, customerBasket.Items.Count()); _basketRepositoryMock.Verify(); _identityServiceMock.Verify(); _serviceBusMock.Verify(); }
public async Task AddItem_basket_invalid_model() { //arrange var customerId = "123"; BasketItem input = new BasketItem("004", "004", "produto 004", 45.67m, 4); _basketRepositoryMock .Setup(c => c.AddBasketAsync(customerId, It.IsAny <BasketItem>())) .ThrowsAsync(new KeyNotFoundException()); var controller = new BasketController( _basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object, _loggerMock.Object, _configurationMock.Object); controller.ModelState.AddModelError("ProductId", "Required"); //act ActionResult <CustomerBasket> actionResult = await controller.AddItem(customerId, input); //assert Assert.IsType <BadRequestObjectResult>(actionResult.Result); }
public async Task AddItem_basket_notfound() { //arrange var customerId = "123"; BasketItem input = new BasketItem("004", "004", "produto 004", 45.67m, 4); _basketRepositoryMock .Setup(c => c.AddBasketAsync(customerId, It.IsAny <BasketItem>())) .ThrowsAsync(new KeyNotFoundException()); var controller = new BasketController( _basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object, _loggerMock.Object, _configurationMock.Object); //act ActionResult <CustomerBasket> actionResult = await controller.AddItem(customerId, input); //assert NotFoundObjectResult notFoundObjectResult = Assert.IsType <NotFoundObjectResult>(actionResult.Result); Assert.Equal(customerId, notFoundObjectResult.Value); }