Esempio n. 1
0
        public void Add_IsNull_ExceptionThrown()
        {
            var sut = new ShoppingBasketService(_productService.Object);

            // Assert
            Assert.Throws <NullReferenceException>(() => sut.AddProducts(null));
        }
Esempio n. 2
0
        public void RandomProductFor_PercentageDiscount_CalculateAppliedDiscount_With10PercentDiscount(List <Product> products, PercentageDiscount percentageDiscount)
        {
            var discountRate = 0.10m;

            // Arrange
            percentageDiscount._percentage = discountRate;
            var productNames = products.Select(x => x.Name).ToList();

            productNames.Add(percentageDiscount._discountedProduct.Name);

            var disconts = new List <IDiscount>();

            disconts.Add(percentageDiscount);

            _productService.Setup(x => x.GetProductByName(percentageDiscount._discountedProduct.Name)).Returns(percentageDiscount._discountedProduct);

            foreach (var item in products)
            {
                _productService.Setup(x => x.GetProductByName(item.Name)).Returns(item);
            }

            var sut    = new ShoppingBasketService(_productService.Object);
            var basket = sut.AddProducts(productNames.ToArray());
            var sutD   = new DiscountService();

            // Act
            var result = sutD.CalculateBasketDiscounts(basket, disconts);

            var subTotalPrice  = basket.Sum(item => item.Product.Price * item.Quantity);
            var totalPrice     = subTotalPrice - result.Sum(item => item.Amount);
            var discontMessage = string.Join(" --- ", result.Select(x => x.Description));

            // Assert
            Assert.Equal(subTotalPrice - totalPrice, percentageDiscount._discountedProduct.Price * percentageDiscount._percentage);
        }
Esempio n. 3
0
        public void GetProductByName_Is_Null_ExceptionThrown(Product product, string exeptedProductName)
        {
            // Arrange
            var productName  = product.Name;
            var productNames = new string[] { exeptedProductName };

            _productService.Setup(x => x.GetProductByName(productName)).Returns(product);
            var sut = new ShoppingBasketService(_productService.Object);

            // Assert
            Assert.Throws <NotSupportedException>(() => sut.AddProducts(productNames));
        }
Esempio n. 4
0
        public void Can_Add_One_Product(Product product)
        {
            // Arrange
            var productName  = product.Name;
            var productNames = new string[] { productName };

            _productService.Setup(x => x.GetProductByName(productName)).Returns(product);
            var sut = new ShoppingBasketService(_productService.Object);

            // Act
            var result = sut.AddProducts(productNames);

            // Assert
            Assert.Equal(result.Count, 1);
        }
Esempio n. 5
0
        public void Add_Product_And_Check_Quantity(Product product)
        {
            // Arrange
            var productName  = product.Name;
            var productNames = new string[] { productName, productName };

            _productService.Setup(x => x.GetProductByName(productName)).Returns(product);
            var sut = new ShoppingBasketService(_productService.Object);

            // Act
            var result          = sut.AddProducts(productNames);
            var productQuantity = result.SingleOrDefault(x => x.Product.Name == productName);

            // Assert
            Assert.Equal(productQuantity.Quantity, 2);
        }
Esempio n. 6
0
        public void AddProducts_And_CheckProductsCount(List <Product> products)
        {
            // Arrange
            var productNames = products.Select(x => x.Name).ToArray();

            foreach (var item in products)
            {
                _productService.Setup(x => x.GetProductByName(item.Name)).Returns(item);
            }

            var sut = new ShoppingBasketService(_productService.Object);

            // Act
            var result = sut.AddProducts(productNames);

            // Assert
            Assert.Equal(result.Count, products.Count);
        }