public async Task AddProductAsyncUserAuthenticatedShouldReturnFalseWithExistingShoppingCartIdAndProductIdUsingMoq()
        {
            var repository = new Mock <IRepository <ShoppingCartProduct> >();

            var store       = new Mock <IUserStore <ApplicationUser> >();
            var userManager = new Mock <UserManager <ApplicationUser> >(store.Object, null, null, null, null, null, null, null, null);

            var users = new List <ApplicationUser>
            {
                new ApplicationUser {
                    Id = "TestUserId1", ShoppingCartId = "TestShoppingCartId1"
                },
            };

            var shoppingCartProducts = new List <ShoppingCartProduct>
            {
                new ShoppingCartProduct {
                    Id = "TestId1", ProductId = "TestProductId1", ShoppingCartId = "TestShoppingCartId1"
                },
            };

            repository.Setup(r => r.All()).Returns(shoppingCartProducts.AsQueryable());

            userManager.Setup(r => r.FindByIdAsync(It.IsAny <string>()))
            .Returns(async(string userId) => await Task.FromResult <ApplicationUser>(users.FirstOrDefault(x => x.Id == userId)));

            var service = new ShoppingCartService(repository.Object, userManager.Object, null);

            Assert.False(await service.AddProductAsync(true, null, "TestUserId1", "TestProductId1"));

            repository.Verify(x => x.All(), Times.Once);

            userManager.Verify(x => x.FindByIdAsync(It.IsAny <string>()), Times.Once);
        }
Esempio n. 2
0
        public async Task <IActionResult> Add(int productId, int amount)
        {
            if (await _shoppingCartService.AddProductAsync(productId, amount, this.User) == null)
            {
                return(BadRequest());
            }

            return(RedirectToAction("Index"));
        }
        public async Task AddProductAsyncUserAuthenticatedShouldWorkCorrectlyUsingMoq()
        {
            var shoppingCartRepository = new Mock <IRepository <ShoppingCartProduct> >();
            var productsService        = new Mock <IProductsService>();

            var store       = new Mock <IUserStore <ApplicationUser> >();
            var userManager = new Mock <UserManager <ApplicationUser> >(store.Object, null, null, null, null, null, null, null, null);

            var users = new List <ApplicationUser>
            {
                new ApplicationUser {
                    Id = "TestUserId1", ShoppingCartId = "TestShoppingCartId1"
                },
            };

            var shoppingCartProducts = new List <ShoppingCartProduct>
            {
                new ShoppingCartProduct {
                    Id = "TestId1", ProductId = "TestProductId1", ShoppingCartId = "TestShoppingCartId1"
                },
            };

            var products = new List <Product>
            {
                new Product {
                    Id = "TestProductId1"
                },
                new Product {
                    Id = "TestProductId2"
                },
            };

            shoppingCartRepository.Setup(r => r.All()).Returns(shoppingCartProducts.AsQueryable());
            shoppingCartRepository.Setup(r => r.AddAsync(It.IsAny <ShoppingCartProduct>())).Callback((ShoppingCartProduct item) => shoppingCartProducts.Add(item));
            shoppingCartRepository.Setup(r => r.SaveChangesAsync()).Verifiable();

            productsService.Setup(r => r.HasProduct(It.IsAny <string>())).Returns((string id) => products.Any(x => x.Id == id));

            userManager.Setup(r => r.FindByIdAsync(It.IsAny <string>()))
            .Returns(async(string userId) => await Task.FromResult <ApplicationUser>(users.FirstOrDefault(x => x.Id == userId)));

            var service = new ShoppingCartService(shoppingCartRepository.Object, userManager.Object, productsService.Object);

            Assert.True(await service.AddProductAsync(true, null, "TestUserId1", "TestProductId2"));
            Assert.Equal(2, shoppingCartProducts.Count);
            Assert.Equal("TestProductId2", shoppingCartProducts.LastOrDefault().ProductId);
            Assert.Equal("TestShoppingCartId1", shoppingCartProducts.LastOrDefault().ShoppingCartId);
            Assert.Equal(1, shoppingCartProducts.LastOrDefault().Quantity);

            shoppingCartRepository.Verify(x => x.All(), Times.Once);
            shoppingCartRepository.Verify(x => x.AddAsync(It.IsAny <ShoppingCartProduct>()), Times.Once);
            shoppingCartRepository.Verify(x => x.SaveChangesAsync());

            productsService.Verify(x => x.HasProduct(It.IsAny <string>()), Times.Once);

            userManager.Verify(x => x.FindByIdAsync(It.IsAny <string>()), Times.Once);
        }
Esempio n. 4
0
 public async Task <ActionResult> AddItem(int id)
 {
     try
     {
         var orderDetail = await _shoppingCartService.AddProductAsync(id);
     }
     catch (ProductAlreadyInShoppingCartException) { }
     catch (NotFoundException e)
     {
         return(HttpNotFound(e.Message));
     }
     catch (Exception e)
     {
         return(this.JError(400, e.Message));
     }
     return(this.JOk());
 }