Exemplo n.º 1
0
        public void Pass_Through_Variable_Discounts()
        {
            //准备
            Mock <IDiscountHelper> mock = new Mock <IDiscountHelper>();

            mock.Setup(t => t.ApplyDiscount(It.IsAny <decimal>()))
            .Returns <decimal>(total => total);
            mock.Setup(t => t.ApplyDiscount(It.Is <decimal>(v => v == 0)))
            .Throws <ArgumentOutOfRangeException>();
            mock.Setup(t => t.ApplyDiscount(It.Is <decimal>(v => v > 100)))
            .Returns <decimal>(total => total * 0.9m);
            mock.Setup(t => t.ApplyDiscount(It.IsInRange <decimal>(10, 100, Range.Inclusive)))
            .Returns <decimal>(total => total - 5);
            var target = new LinqValueCaculator(mock.Object);

            //动作

            decimal fiveDollarDiscount        = target.ValueProducts(CreateProduct(5));
            decimal tenDollarDiscount         = target.ValueProducts(CreateProduct(10));
            decimal fiftyDollarDiscount       = target.ValueProducts(CreateProduct(50));
            decimal hundredDollarDiscount     = target.ValueProducts(CreateProduct(100));
            decimal fiveHundredDollarDiscount = target.ValueProducts(CreateProduct(500));

            //断言
            Assert.AreEqual(5, fiveDollarDiscount, "5 fail");
            Assert.AreEqual(5, tenDollarDiscount, "10 fail");
            Assert.AreEqual(45, fiftyDollarDiscount, "50 fail");
            Assert.AreEqual(95, hundredDollarDiscount, "100 fail");
            Assert.AreEqual(450, fiveHundredDollarDiscount, "500 fail");
            target.ValueProducts(CreateProduct(0));
        }
Exemplo n.º 2
0
        public void Sun_Products_Correctly()
        {
            //准备
            Mock <IDiscountHelper> mock = new Mock <IDiscountHelper>();

            mock.Setup(t => t.ApplyDiscount(It.IsAny <decimal>())).Returns <decimal>(total => total);
            //var discounter = new MinmumDiscountHelper();
            var target    = new LinqValueCaculator(mock.Object);
            var goalTotal = products.Sum(t => t.Price);

            //动作
            var result = target.ValueProducts(products);

            Assert.AreEqual(goalTotal, result);
        }