コード例 #1
0
        public int Tests(int aItemCount = 0,
                         int bItemCount = 0,
                         int cItemCount = 0,
                         int dItemCount = 0)
        {
            var aSku = new DiscountedSku(new Sku('A', 50), 3, 130);
            var bSku = new DiscountedSku(new Sku('B', 30), 2, 45);
            var cSku = new Sku('C', 20);
            var dSku = new Sku('D', 15);

            var pricing  = new Pricing(aSku, bSku, cSku, dSku);
            var checkout = new store.Checkout(pricing);

            void callScan(string item, int itemCount)
            {
                var counter = itemCount;

                while (counter != 0)
                {
                    checkout.Scan(item);
                    counter--;
                }
            }

            callScan("A", aItemCount);
            callScan("B", bItemCount);
            callScan("C", cItemCount);
            callScan("D", dItemCount);

            return(checkout.GetTotalPrice());
        }
コード例 #2
0
        public void ItemDoesNotExist_ThrowException()
        {
            var pricingMock = new Mock <IPricing>();

            pricingMock.Setup(x => x.TryGetSku(It.IsAny <string>(), out It.Ref <ISku> .IsAny)).Returns(false);
            var target = new store.Checkout(pricingMock.Object);

            Assert.Throws <ArgumentException>(() => target.Scan("A"));
        }
コード例 #3
0
        public int Valid(
            int aItemCount = 0,
            int bItemCount = 0,
            int cItemCount = 0,
            int dItemCount = 0)
        {
            int getTotalForA()
            => ((aItemCount % 3) * 50) + ((aItemCount / 3) * 130);

            var aSkuMock = new Mock <ISku>();

            aSkuMock
            .Setup(x => x.Name)
            .Returns('A');
            aSkuMock
            .Setup(x => x.GetPrice(It.IsAny <IItemCount>()))
            .Returns(getTotalForA());

            int getTotalForB()
            => ((bItemCount % 2) * 30) + ((bItemCount / 2) * 45);

            var bSkuMock = new Mock <ISku>();

            bSkuMock
            .Setup(x => x.Name)
            .Returns('B');
            bSkuMock
            .Setup(x => x.GetPrice(It.IsAny <IItemCount>()))
            .Returns(getTotalForB());

            int getTotalForC()
            => cItemCount * 20;

            var cSkuMock = new Mock <ISku>();

            cSkuMock
            .Setup(x => x.Name)
            .Returns('C');
            cSkuMock
            .Setup(x => x.GetPrice(It.IsAny <IItemCount>()))
            .Returns(getTotalForC());

            int getTotalForD()
            => dItemCount * 15;

            var dSkuMock = new Mock <ISku>();

            dSkuMock
            .Setup(x => x.Name)
            .Returns('D');
            dSkuMock
            .Setup(x => x.GetPrice(It.IsAny <IItemCount>()))
            .Returns(getTotalForD());

            var availableSkus = new[]
            {
                aSkuMock.Object,
                bSkuMock.Object,
                cSkuMock.Object,
                dSkuMock.Object
            }.ToList().AsReadOnly();



            var pricingMock = new Mock <IPricing>();

            pricingMock.Setup(x => x.AvailableSkus)
            .Returns(availableSkus);
            void SetupTryGetSku(string item, ISku sku)
            {
                pricingMock
                .Setup(x => x.TryGetSku(item, out It.Ref <ISku> .IsAny))
                .Callback(new TryGetSkuCallBack((string _item, out ISku _sku) =>
                {
                    _sku = sku;
                }))
                .Returns(true);
            }

            SetupTryGetSku("A", aSkuMock.Object);
            SetupTryGetSku("B", bSkuMock.Object);
            SetupTryGetSku("C", cSkuMock.Object);
            SetupTryGetSku("D", dSkuMock.Object);

            var target = new store.Checkout(pricingMock.Object);

            void callScan(string item, int itemCount)
            {
                var counter = itemCount;

                while (counter != 0)
                {
                    target.Scan(item);
                    counter--;
                }
            }

            callScan("A", aItemCount);
            callScan("B", bItemCount);
            callScan("C", cItemCount);
            callScan("D", dItemCount);

            return(target.GetTotalPrice());
        }