예제 #1
0
        public void Should_CalculateCorrectTotalPriceWithoutDiscounts_When_ItemsScannedIncrementally()
        {
            // Arrange
            var priceSystemMock = new Mock <IDiscountCalculator>();

            priceSystemMock.Setup(x => x.CalculateTotalDiscount(It.IsAny <IEnumerable <ProductItem> >()));

            var checkout = new Checkout(priceSystemMock.Object);

            var item1 = new ProductItem {
                Id = 0, Price = 10
            };
            var item2 = new ProductItem {
                Id = 1, Price = 20
            };
            // Act
            // Assert
            var runningTotalPrice = 0;

            checkout.Scan(item1);
            runningTotalPrice = checkout.CalculateTotalPrice();
            Assert.AreEqual(10, runningTotalPrice);

            checkout.Scan(item2);
            runningTotalPrice = checkout.CalculateTotalPrice();
            Assert.AreEqual(30, runningTotalPrice);

            checkout.Scan(item1);
            runningTotalPrice = checkout.CalculateTotalPrice();
            Assert.AreEqual(40, runningTotalPrice);

            checkout.Scan(item1);
            runningTotalPrice = checkout.CalculateTotalPrice();
            Assert.AreEqual(50, runningTotalPrice);
        }
예제 #2
0
        public void Should_CorrectlyCalculatePrice_When_ThereIsOneOfEachItem()
        {
            // Arrange
            var priceSystemMock = new Mock <IDiscountCalculator>();

            priceSystemMock.Setup(x => x.CalculateTotalDiscount(
                                      It.IsAny <int>(), It.IsAny <int>()
                                      )).Returns(0);

            var checkout = new Checkout(priceSystemMock.Object);

            var item1 = new ProductItem {
                Id = 0, Price = 10
            };
            var item2 = new ProductItem {
                Id = 1, Price = 20
            };
            var item3 = new ProductItem {
                Id = 2, Price = 5
            };

            var items = new List <ProductItem> {
                item1, item2, item3
            };

            checkout.ScanBasket(items);

            // Act
            var result = checkout.CalculateTotalPrice();

            // Assert
            Assert.AreEqual(35, result);
        }
예제 #3
0
        public void Should_ApplyDiscount_When_DiscountApplicable()
        {
            // Arrange
            var priceSystemMock = new Mock <IDiscountCalculator>();

            priceSystemMock.Setup(x => x.CalculateTotalDiscount(It.IsAny <IEnumerable <ProductItem> >()));

            var checkout = new Checkout(priceSystemMock.Object);

            var item = new ProductItem {
                Id = 0, Price = 3
            };

            checkout.Scan(item);
            checkout.Scan(item);
            checkout.Scan(item);

            // Act
            checkout.CalculateTotalPrice();

            // Assert
            priceSystemMock.Verify(x => x.CalculateTotalDiscount(
                                       It.Is <IEnumerable <ProductItem> >(items => items.All(checkout.BasketItems.Contains))),
                                   Times.Once);
        }
예제 #4
0
        public void Should_CorrectlyCalculatePrice_When_DiscountIsApplied()
        {
            // Arrange
            var priceSystemMock = new Mock <IDiscountCalculator>();

            priceSystemMock.Setup(x => x.CalculateTotalDiscount(
                                      It.IsAny <IEnumerable <ProductItem> >()
                                      )).Returns(20);

            var checkout = new Checkout(priceSystemMock.Object);

            var item = new ProductItem {
                Id = 0, Price = 10
            };

            checkout.Scan(item);
            checkout.Scan(item);
            checkout.Scan(item);

            // Act
            var result = checkout.CalculateTotalPrice();

            // Assert
            Assert.AreEqual(10, result);
        }
예제 #5
0
        public void GivenListOfItems_WhenListHasNoItem_ReturnsZeroTotalCost()
        {
            //arrange
            List <item> listWithNoItems = new List <item>();

            //act
            var sut = new Checkout();


            //Assert
            Assert.Equal(0, sut.CalculateTotalPrice(listWithNoItems));
        }
예제 #6
0
        public void GivenListOfItems_WhenListIsNull_ThrowsArgumentException()
        {
            //arrange
            List <item> listWithNoItems = null;

            //act
            var sut = new Checkout();


            //Assert
            Assert.Throws <ArgumentException>(() => { sut.CalculateTotalPrice(listWithNoItems); });
        }
예제 #7
0
        public void GivenListOfItems_WhenItemssAreValid_ReturnsTotalCost()
        {
            //arrange
            var     listOfItems            = TestVariables.GetFakeListOfItems();
            Decimal totalApplePriceInList  = listOfItems.Where(x => x.ItemName.ToLower() == "apple").Sum(y => y.ItemPrice);
            Decimal totalOrangePriceInList = listOfItems.Where(x => x.ItemName.ToLower() == "orange").Sum(y => y.ItemPrice);
            var     totalPriceInLIst       = listOfItems.Sum(x => x.ItemPrice);

            var sut = new Checkout();
            //add


            //act
            var result = sut.CalculateTotalPrice(listOfItems);

            //Assert
            Assert.Equal((Decimal)totalPriceInLIst, (Decimal)result);
        }
예제 #8
0
        public void Should_ReturnPriceOf0_When_BasketIsEmpty()
        {
            // Arrange
            var priceSystemMock = new Mock <IDiscountCalculator>();

            priceSystemMock.Setup(x => x.CalculateTotalDiscount(
                                      It.IsAny <int>(), It.IsAny <int>()
                                      )).Returns(0);

            var checkout = new Checkout(priceSystemMock.Object);

            checkout.ScanBasket(new List <ProductItem>());
            // Act
            var result = checkout.CalculateTotalPrice();

            // Assert
            Assert.AreEqual(0, result);
        }