public void AddProductToBasketTest() { // Given Basket's and product's id var basketId = "BaksetsId"; var productid = "ProductsId"; // Given Task with true value Task <bool> taskGetBasketProducts = Task.FromResult(true); mockedBasketRepo.Setup(repo => repo.AddProductToBasket( // When called with basket's and product's id It.Is <string>(baskId => baskId == basketId), It.Is <string>(prodId => prodId == productid) )).Returns(taskGetBasketProducts); // When we call method AddProductToBasket with Basket's and Product's Id var response = basketService.AddProductToBasket(basketId, productid); // Then we expect response to be true Assert.IsTrue(response); }
public async Task ShouldAddProductToBasket() { var created = await basketService.CreateBasket(); await basketService.AddProductToBasket(created.Id, 42); var basket = await basketService.GetById(created.Id); Assert.Equal(1, basket.Count); Assert.Equal(new Money(13.76, SE), basket.Total); Assert.Equal(new Money(13.76, SE) * 1.25, basket.TotalWithVat); Assert.Equal("Product with difficult characaters: ��", basket.Products.Single().Name); }
public ActionResult AddPdToBasket(int Pid) { const int One = 1; bool isSuccessful = bs.AddProductToBasket(this.HttpContext, Pid, One); if (isSuccessful) { return(RedirectToAction("BasketSummary")); } else { return(RedirectToAction("Index")); } }
public async Task ShouldThrowForAddNonExistingProductToBasket() { const string BasketId = "My basket id"; var repositoryMock = new Mock <IRepository>(MockBehavior.Strict); var productServiceMock = new Mock <IProductService>(MockBehavior.Strict); productServiceMock.Setup(productService => productService.GetById(42)).ThrowsAsync(new InvalidOperationException()); var service = new BasketService(productServiceMock.Object, repositoryMock.Object); await Assert.ThrowsAsync <InvalidOperationException>(() => service.AddProductToBasket(BasketId, 42)); repositoryMock.VerifyAll(); }
public async Task ShouldThrowForNonExistingProduct() { var repositoryMock = new Mock <IRepository>(MockBehavior.Strict); repositoryMock .Setup(repository => repository.GetById(It.IsAny <string>())) .ThrowsAsync(new InvalidOperationException()); var productServiceMock = new Mock <IProductService>(MockBehavior.Strict); productServiceMock.Setup(productService => productService.GetById(42)).ReturnsAsync(new Product(42, "My Product", Money.Zero(SE))); var service = new BasketService(productServiceMock.Object, repositoryMock.Object); await Assert.ThrowsAsync <InvalidOperationException>(() => service.AddProductToBasket("a basket id", 42)); repositoryMock.VerifyAll(); }
public async Task ShouldAddProductToBasket() { const string BasketId = "My basket id"; var entity = new BasketEntity { Id = BasketId }; var product = new Product(42, "What name?", new Money(13.76, SE)); var repositoryMock = new Mock <IRepository>(MockBehavior.Strict); repositoryMock .Setup(repository => repository.GetById(BasketId)) .ReturnsAsync(entity); repositoryMock .Setup(repository => repository.InsertOrUpdate(It.IsAny <BasketEntity>())) .ReturnsAsync(entity); var productServiceMock = new Mock <IProductService>(MockBehavior.Strict); productServiceMock.Setup(productService => productService.GetById(42)).ReturnsAsync(product); var service = new BasketService(productServiceMock.Object, repositoryMock.Object); await service.AddProductToBasket(BasketId, 42); repositoryMock.VerifyAll(); repositoryMock.Verify(repository => repository.InsertOrUpdate( It.Is <BasketEntity>(item => item.Id == BasketId && item.Items.Single().Cost.Units == product.Cost.Units && item.Items.Single().Id == 42)), Times.Once); productServiceMock.VerifyAll(); }
public IActionResult AddProduct(int userId, [FromBody] BasketDTO.Post model) { _basketService.AddProductToBasket(userId, model.Name); return(Ok()); }