public void SubmitCart([FromBody] CartModel model) { var cart = queryService.GetCartByUserId(model.UserEmail).Where(c => c.ProductId == model.ProductId).FirstOrDefault(); var entity = mapper.Map <order.commandservice.Models.Cart>(model); if (cart != null) { entity.Id = cart.Id; entity.Quantity = cart.Quantity + entity.Quantity; } entity.Price = entity.Quantity * entity.Price; commandService.SaveCart(entity); }
public void SaveCartTest() { var userid = "*****@*****.**"; var carts = new List <Cart>().AsQueryable(); var cartMock = new Mock <DbSet <Cart> >(); cartMock.As <IQueryable <Cart> >().Setup(m => m.Provider).Returns(carts.Provider); cartMock.As <IQueryable <Cart> >().Setup(m => m.Expression).Returns(carts.Expression); cartMock.As <IQueryable <Cart> >().Setup(m => m.ElementType).Returns(carts.ElementType); cartMock.As <IQueryable <Cart> >().Setup(m => m.GetEnumerator()).Returns(carts.GetEnumerator()); dbContext.Setup(x => x.Cart).Returns(cartMock.Object); command.SaveCart(new Cart { ProductId = 30, UserId = userid }); cartMock.Verify(m => m.Update(It.IsAny <Cart>()), Times.Once); dbContext.Verify(m => m.SaveChanges(), Times.Once); }