Beispiel #1
0
        public void Calculate_Cart_Total()
        {
            // Arrange
            LaptopModel laptop1 = new LaptopModel {
                Id = 1, Name = "Laptop1", Price = 1
            };
            LaptopModel laptop2 = new LaptopModel {
                Id = 2, Name = "Laptop2", Price = 1
            };

            var cart = new CartService();

            // Act
            cart.AddItem(laptop1, 3);
            cart.AddItem(laptop2, 2);
            cart.AddItem(laptop1, 5);
            decimal result = cart.ComputeTotalValue();

            // Assert
            Assert.Equal(10, result);
        }
        public async void Can_Add_Quantity_For_Existing_Lines()
        {
            var p1 = new ProductDto(1, "P1", null, 0, null);
            var p2 = new ProductDto(2, "P2", null, 0, null);

            CartService target = new CartService();

            await target.AddItem(p1, 1);

            await target.AddItem(p2, 1);

            await target.AddItem(p1, 10);

            var lines = target.Lines;

            var results = lines.OrderBy(c => c.ProductDto.Id).ToArray();

            Assert.Equal(2, results.Length);
            Assert.Equal(11, results[0].Quantity);
            Assert.Equal(1, results[1].Quantity);
        }
Beispiel #3
0
        public void Can_Clear_Contents()
        {
            // Arrange
            Product p1 = new Product {
                Id = 1, Name = "P1", Price = 100M
            };
            Product p2 = new Product {
                Id = 2, Name = "P2", Price = 50M
            };

            CartService target = new CartService();

            // Act
            target.AddItem(p1, 1);
            target.AddItem(p2, 1);
            target.AddItem(p1, 3);

            target.Clear();

            // Assert
            Assert.Empty(target.Lines);
        }
        public ViewResult Index([FromQuery] string name = "SinjulMSBH")
        {
            Logger.LogInformation("Hello, {Name} .. !!!!", name);

            DiagnosticContext.Set("IndexCallCount", Interlocked.Increment(ref _callCount));

            IEnumerable <ILoggerProvider> providers = LoggerProvider.Providers;

            ShoppingCartService.AddItem("SinjulMSBH", 130);
            CartService.AddItem("JackSlater", 220);

            return(View());
        }
Beispiel #5
0
        public void Can_Clear_Content()
        {
            // Arrange
            LaptopModel laptop1 = new LaptopModel {
                Id = 1, Name = "Laptop1", Price = 100
            };
            LaptopModel laptop2 = new LaptopModel {
                Id = 2, Name = "Laptop2", Price = 55
            };

            var cart = new CartService();

            // Act
            cart.AddItem(laptop1, 1);
            cart.AddItem(laptop2, 1);
            cart.AddItem(laptop1, 5);
            cart.Clear();

            // Assert

            Assert.Equal(0, cart.GetCartLine().Count);
        }
Beispiel #6
0
        public void Can_Add_Quantity_For_Existing_Lines()
        {
            //Arrange
            LaptopModel laptop1 = new LaptopModel {
                Id = 1, Name = "Laptop1"
            };
            LaptopModel laptop2 = new LaptopModel {
                Id = 2, Name = "Laptop2"
            };
            var cart = new CartService();

            //Act
            cart.AddItem(laptop1, 1);
            cart.AddItem(laptop2, 1);
            cart.AddItem(laptop1, 5);
            var result = cart.GetCartLine().OrderBy(c => c.Laptop.Id).ToList();

            //Assert
            Assert.Equal(2, result.Count);
            Assert.Equal(6, result[0].Quantity);
            Assert.Equal(1, result[1].Quantity);
        }
Beispiel #7
0
        public void Calculate_Cart_Total()
        {
            // Arrange
            Product p1 = new Product {
                Id = 1, Name = "P1", Price = 100M
            };
            Product p2 = new Product {
                Id = 2, Name = "P2", Price = 50M
            };

            CartService target = new CartService();

            // Act
            target.AddItem(p1, 1);
            target.AddItem(p2, 1);
            target.AddItem(p1, 3);

            decimal result = target.ComputeTotalValue();

            // Assert
            Assert.Equal(450M, result);
        }
Beispiel #8
0
        public void Can_Add_New_Lines()
        {
            // Arrange
            Product p1 = new Product {
                Id = 1, Name = "P1"
            };
            Product p2 = new Product {
                Id = 2, Name = "P2"
            };

            CartService target = new CartService();

            // Act
            target.AddItem(p1, 1);
            target.AddItem(p2, 1);

            CartLine[] results = target.Lines.ToArray();

            // Assert
            Assert.Equal(2, results.Length);
            Assert.Equal(p1, results[0].Product);
            Assert.Equal(p2, results[1].Product);
        }
        public async void Can_Remove_Line()
        {
            var p1 = new ProductDto(1, "P1", null, 0, null);
            var p2 = new ProductDto(2, "P2", null, 0, null);
            var p3 = new ProductDto(3, "P3", null, 0, null);

            CartService target = new CartService();

            await target.AddItem(p1, 1);

            await target.AddItem(p2, 3);

            await target.AddItem(p3, 5);

            await target.AddItem(p2, 1);

            await target.RemoveLine(p2);

            var lines = target.Lines;

            Assert.Empty(lines.Where(c => c.ProductDto == p2));
            Assert.Equal(2, lines.Count());
        }
Beispiel #10
0
        public void Can_Add_New_Lines()
        {
            // Arrange
            LaptopModel laptop1 = new LaptopModel {
                Id = 1, Name = "Laptop1"
            };
            LaptopModel laptop2 = new LaptopModel {
                Id = 2, Name = "Laptop2"
            };

            var cart = new CartService();

            // Act
            cart.AddItem(laptop1, 1);
            cart.AddItem(laptop2, 1);

            var results = cart.GetCartLine();

            // Assert
            Assert.Equal(2, results.Count);
            Assert.Equal(1, results[0].Quantity);
            Assert.Equal(1, results[1].Quantity);
        }
Beispiel #11
0
        public void Can_Add_Quantity_For_Existing_Line()
        {
            // Arrange
            Product p1 = new Product {
                Id = 1, Name = "P1"
            };
            Product p2 = new Product {
                Id = 2, Name = "P2"
            };

            CartService target = new CartService();

            // Act
            target.AddItem(p1, 1);
            target.AddItem(p2, 1);
            target.AddItem(p1, 10);

            CartLine[] results = target.Lines.ToArray();

            // Assert
            Assert.Equal(2, results.Length);
            Assert.Equal(11, results[0].Quantity);
            Assert.Equal(1, results[1].Quantity);
        }
        public void Can_Checkout_And_Submit_Order()
        {
            // Arrange - create a mock order repository
            Mock <IRepository <Order> > mock = new Mock <IRepository <Order> >();
            // Arrange - create a cart with one item
            CartService cart = new CartService();

            cart.AddItem(new Product(), 1);
            // Arrange - create an instance of the controller
            OrderController target = new OrderController(mock.Object, cart);

            // Act - try to checkout
            RedirectToActionResult result =
                target.Checkout(new Order()) as RedirectToActionResult;

            // Assert - check that the order has been stored
            mock.Verify(m => m.Update(It.IsAny <Order>()), Times.Once);
            // Assert - check that the method is redirecting to the Completed action
            Assert.Equal("Completed", result.ActionName);
        }
Beispiel #13
0
        public void NotCreateCartIfExistingOneFound()
        {
            Guid sessionId = Guid.NewGuid();

            using (var context = new StoreContext(options))
            {
                context.ShoppingCart.Add(new ShoppingCart {
                    SessionId = sessionId
                });
                context.SaveChanges();
            }

            using (var context = new StoreContext(options))
            {
                var cartService = new CartService(context);
                var result      = cartService.AddItem(new AddItemCommand {
                    ProductId = 1, SessionId = sessionId
                });
                Assert.Equal(sessionId, result.SessionId);
            }
        }
Beispiel #14
0
        public void UpdateCartLineQuantityWhenAddedMoreThanOnce()
        {
            Guid sessionId = Guid.NewGuid();

            using (var context = new StoreContext(options))
            {
                context.ShoppingCart.Add(new ShoppingCart {
                    SessionId = sessionId
                });
                context.SaveChanges();
            }

            using (var context = new StoreContext(options))
            {
                var cartService = new CartService(context);
                cartService.AddItem(new AddItemCommand {
                    ProductId = 1, SessionId = sessionId
                });
            }

            using (var context = new StoreContext(options))
            {
                var cartService = new CartService(context);
                cartService.AddItem(new AddItemCommand {
                    ProductId = 1, SessionId = sessionId
                });
            }

            using (var context = new StoreContext(options))
            {
                var cart = context
                           .ShoppingCart
                           .Include(x => x.Items)
                           .SingleOrDefault(x => x.SessionId == sessionId);

                cart.Items.Count(x => x.ProductId == 1).ShouldBe(1);
                cart.Items.SingleOrDefault(x => x.ProductId == 1).Quantity.ShouldBe(2);
            }
        }
        public void AddItem_Test()
        {
            var productDataStore = new List <Product>
            {
                new Product {
                    Id = "1", Name = "Iphone XS Max", Price = 43500
                },
                new Product {
                    Id = "2", Name = "Ipad pro", Price = 35000
                },
                new Product {
                    Id = "3", Name = "Samsung Galaxy S10", Price = 38000
                }
            };
            var cartDataStore = new List <Cart>
            {
                new Cart {
                    CartItems = new List <CartItem>()
                }
            };

            var cartService = new CartService(productDataStore, cartDataStore);

            cartService.AddItem("3", 1);

            var expected = new List <Cart>
            {
                new Cart {
                    CartItems = new List <CartItem> {
                        new CartItem {
                            ProductId = "3", Quantity = 1
                        }
                    }
                }
            };

            cartDataStore.Should().BeEquivalentTo(expected);
        }
        public void Cannot_Checkout_Invalid_ShippingDetails()
        {
            // Arrange - create a mock order repository
            Mock <IRepository <Order> > mock = new Mock <IRepository <Order> >();
            // Arrange - create a cart with one item
            CartService cart = new CartService();

            cart.AddItem(new Product(), 1);
            // Arrange - create an instance of the controller
            OrderController target = new OrderController(mock.Object, cart);

            // Arrange - add an error to the model
            target.ModelState.AddModelError("error", "error");

            // Act - try to checkout
            ViewResult result = target.Checkout(new Order()) as ViewResult;

            // Assert - check that the order hasn't been passed stored
            mock.Verify(m => m.Update(It.IsAny <Order>()), Times.Never);
            // Assert - check that the method is returning the default view
            Assert.True(string.IsNullOrEmpty(result.ViewName));
            // Assert - check that I am passing an invalid model to the view
            Assert.False(result.ViewData.ModelState.IsValid);
        }
 public void AddItem(string productId, int quantity)
 {
     cartService.AddItem(productId, quantity);
 }