Example #1
0
        public void Pass_Trough_Variable_Discounts()
        {
            // arrange
            var mock = new Mock <IDiscountHelper>();

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

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

            // assert
            Assert.AreEqual(5, fiveDollarDiscount);
            Assert.AreEqual(10, tenDollarDiscount);
            Assert.AreEqual(50, fiftyDollarDiscount);
            Assert.AreEqual(100, hundredDollarDiscount);
            Assert.AreEqual(500, fiveHundredDollarDiscount);
        }
Example #2
0
        public void Pass_Through_Variable_Discounts()
        {
            //arrange
            Mock <IDiscountHelper> mock = new Mock <IDiscountHelper>();

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

            //act
            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
            Assert.AreEqual(5, FiveDollarDiscount, "$5 Fail");
            Assert.AreEqual(10, TenDollarDiscount, "$10 Fail");
            Assert.AreEqual(10, FiftyDollarDiscount, "$50 Fail");
            Assert.AreEqual(10, HundredDollarDiscount, "$100 Fail");
            Assert.AreEqual(10, FiveHundredDollarDiscount, "$500 Fail");
        }
        public void Sum_Products_Correctly()
        {
            // arrange
            // makes a new strongly typed Mock<IDiscountHelper> object
            Mock <IDiscountHelper> mock = new Mock <IDiscountHelper>();

            // specifying behavior of object
            mock.Setup(m => m.ApplyDiscount(It.IsAny <decimal>())) // tells Moq to apply the behavior being defined whenever it encounters a decimal
            .Returns <decimal>(total => total);                    // returns defines the results

            var target = new LinqValueCalculator(mock.Object);     // reads the value of the Object property of the Mock<IDiscountHelper> object

            #region Without using Moq

            /* Without Using Moq
             * var discounter = new MinimumDiscountHelper();
             * var target = new LinqValueCalculator(discounter);
             * var goalTotal = products.Sum(e => e.Price);
             */
            #endregion

            // act
            var result = target.ValueProducts(products);

            // assert
            Assert.AreEqual(products.Sum(e => e.Price), result);
            #region Without using Moq
            // Assert.AreEqual(goalTotal, result);
            #endregion

            // benefit of this Moqis that the unit test only checks the behavior of the LinqValueCalculator object and does not depend on any of the real
            // implementations of the IDiscountHelper interface in the Models folder...
            // this  makes it easier to tell where a problem is occuring if a test fails
        }
Example #4
0
        public void Sum_Products_Correctly()
        {
            ////arrange
            //var discounter = new MinimumDiscountHelper();
            //var target = new LinqValueCalculator(discounter);
            //var goalTotal = products.Sum(e => e.Price);

            ////act
            //var result = target.ValueProducts(products);

            ////assert
            //Assert.AreEqual(goalTotal, result);

            //arrange
            Mock <IDiscountHelper> mock = new Mock <IDiscountHelper>();

            mock.Setup(m => m.ApplyDiscount(It.IsAny <decimal>())).Returns <decimal>(total => total);
            var target = new LinqValueCalculator(mock.Object);

            //act
            var result = target.ValueProducts(products);

            //assert
            Assert.AreEqual(products.Sum(e => e.Price), result);
        }
Example #5
0
        public void Sum_Products_Correctly()
        {
            //准备

            #region 使用 Moq

            // 1、创建模仿对象
            Mock <IDiscountHelper> mock = new Mock <IDiscountHelper>();
            // 2、选择方法:Setup,并通过 It 类设置需要的参数信息
            // 3、定义结果:Returns,并用 lambda 表达式在 Return 方法中建立具体行为
            mock.Setup(m => m.ApplyDiscount(It.IsAny <decimal>())).Returns <decimal>(total => total);
            // 4、使用模仿对象:mock.Object,通过获取 Object 属性的值来获取具体实现,如这里的实现是 IDiscountHelper 接口的实现
            var target = new LinqValueCalculator(mock.Object);

            // *** 优点:使用 Moq 会使单元测试只检查 LinqValueCalculator 对象的行为,并不依赖任何 Models 文件夹中 IDiscountHelper 的真实实现。***

            #endregion End 使用 Moq

            //var dicounter = new MinimumDiscountHelper();
            //var target = new LinqValueCalculator(dicounter);
            var goalTotal = _products.Sum(e => e.Price);

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

            //断言
            Assert.AreEqual(goalTotal, result);
        }
Example #6
0
        public void Sum_Products_Correctly()
        //{
        //    var disounter=new MinimumDiscountHelper();
        //   var target = new LinqValueCalculator(disounter);
        //    var goalTotal = products.Sum(e => e.Price);

        {    // Arrange
            //create a mock IDiscountHelper implementation
            Mock <IDiscountHelper> mock = new Mock <IDiscountHelper>();

            //specify the way that MCOK behaves,Setup method to add a method to my mock object.
            //When I call the Setup method, Moq passes me the interface that I have asked it to implement
            //d ApplyDiscount method,  only method in the IDiscountHelper interface, and the method I need to test the
            //LinqValueCalculator class.
            //The It class defines a number of methods that are used with generic type parameters
            mock.Setup(m => m.ApplyDiscount(It.IsAny <decimal>())).Returns <decimal>(total => total);
            //lambda expression, Moq passes me a value of the type I receive in the ApplyDiscount
            //I create a pass-through method , in which I return the value that is passed to the mock
            //ApplyDiscount method without performing any operations on it.


            var target = new LinqValueCalculator(mock.Object);

            // Object property returns an implementation of the IDiscountHelper interface
            //  where the ApplyDiscount method returns the value of the decimal parameter it is passed.
            // action
            var result = target.ValueProducts(products);

            Assert.AreEqual(products.Sum(e => e.Price), result);


            //---------------------- Complex Example----------------------------------
        }
Example #7
0
        public void Sum_Product_Correctly()
        {
            // arrange
            //var discounter = new MinumiumDiscountHelper();
            //var target = new LinqValueCalculator(discounter);
            //var goalTotal = products.Sum(p => p.Price);


            // create strongly typed object:: it can be any type, it only must implement
            // the interface
            Mock <IDiscountHelper> mok = new Mock <IDiscountHelper>();

            // define how the object should behave, so to isolate a single behaviour
            // and keep focused on the unit test
            mok.Setup(m => m.ApplayDiscount(It.IsAny <decimal>()))
            .Returns <decimal>(total => total);

            var target = new LinqValueCalculator(mok.Object);

            // act
            var result = target.ValueProducts(products);

            // Assert
            Assert.AreEqual(products.Sum(p => p.Price), result, "Sum of products is incorrect");
        }
        public void ValueProducts_WorksWithMinimumDiscountHelper()
        {
            // Arrange
            var mock = new Mock <IDiscountHelper>();

            mock.Setup(m => m.ApplyDiscount(It.Is <decimal>(p => p < 0)))
            .Throws <ArgumentOutOfRangeException>();
            mock.Setup(m => m.ApplyDiscount(It.IsInRange <decimal>(0, 9, Range.Inclusive)))
            .Returns <decimal>(p => p);
            mock.Setup(m => m.ApplyDiscount(It.IsInRange <decimal>(10, 100, Range.Inclusive)))
            .Returns <decimal>(p => p - 5);
            mock.Setup(m => m.ApplyDiscount(It.Is <decimal>(p => p > 100)))
            .Returns <decimal>(p => p * 0.9M);

            var calc = new LinqValueCalculator(mock.Object);

            // Act
            var discounted200Dol = calc.ValueProducts(GetProducts(200));
            var discounted100Dol = calc.ValueProducts(GetProducts(100));
            var discounted50Dol  = calc.ValueProducts(GetProducts(50));
            var discounted10Dol  = calc.ValueProducts(GetProducts(10));
            var discounted7Dol   = calc.ValueProducts(GetProducts(7));
            var discounted0Dol   = calc.ValueProducts(GetProducts(0));

            // Assert
            Assert.AreEqual(180, discounted200Dol, "$200 wrong");
            Assert.AreEqual(95, discounted100Dol, "$100 wrong");
            Assert.AreEqual(45, discounted50Dol, "$50 wrong");
            Assert.AreEqual(5, discounted10Dol, "$10 wrong");
            Assert.AreEqual(7, discounted7Dol, "$7 wrong");
            Assert.AreEqual(0, discounted0Dol, "$0 wrong");
            calc.ValueProducts(GetProducts(-5));
        }
Example #9
0
        public void Sum_Products_Correctly()
        {
            // arrange
            //Bước đầu tiên là cho Moq biết mình muốn làm việc với loại
            //đối tượng nào. Moq dựa rất nhiều vào các tham số kiểu, và
            //ta có thể thấy điều này theo cách mà ta nói với Moq rằng
            //ta muốn tạo ra một thực thi giả lập IDiscountHelper

            //chúng ta tạo ra một đối tượng Mock <IDiscountHelper>
            //để nói cho Moq biết type mà Moq sẽ xử lý
            //Đây là interface IDiscountHelper cho unit test của
            //ta, nhưng nó có thể là bất kỳ loại nào mà bạn muốn
            Mock <IDiscountHelper> mock = new Mock <IDiscountHelper>();

            //Chúng cần xác định cách hoạt động của đối tượng vừa tạo.
            //Đây là trọng tâm của quá trình mô phỏng và nó cho phép chúng ta
            //đảm bảo rằng ta thiết lập một hành vi cơ sở trong đối tượng giả,
            //mà ta có thể sử dụng để kiểm tra chức năng của đối tượng đích của
            //ta trong Unit test.
            //Lớp It định nghĩa một số phương thức được sử dụng với các tham số
            //kiểu generic. Trong trường hợp này, ta đã gọi phương thức IsAny
            //bằng cách sử dụng decimal làm kiểu generic. Điều này để Moq
            //áp dụng các hành vi ta định nghĩa bất cứ khi nào ta gọi phương thức
            //ApplyDiscount với bất kỳ giá trị thập phân nào.
            //Phương thức Returns cho phép ta chỉ định kết quả mà Moq sẽ trả về
            //khi ta gọi phương thức giả của mình. Tôi chỉ định loại kết quả bằng
            //cách sử dụng tham số kiểu và chỉ định kết quả bằng cách sử dụng
            //biểu thức lambda.
            //Returns<decimal> nói với Moq rằng ta sẽ trả về một giá trị thập phân.
            //Đối với biểu thức lambda, Moq chuyển cho ta một giá trị của kiểu
            //nhận được trong phương thức ApplyDiscount. Ta tạo ra một phương thức
            //pass-through trong bên dưới, trong đó ta trả về giá trị được truyền vào
            //phương thức ApplyDiscount giả lập mà không thực hiện bất kỳ thao
            //tác nào trên nó.
            mock.Setup(m => m.ApplyDiscount(It.IsAny <decimal>()))
            .Returns <decimal>(total => total);

            //Bước cuối cùng là sử dụng đối tượng giả trong thử nghiệm,
            // bằng cách đọc giá trị của thuộc tính Object của đối tượng
            // Mock <IDiscountHelper>:
            var target = new LinqValueCalculator(mock.Object);
            // act
            var result = target.ValueProducts(products);

            // assert
            Assert.AreEqual(products.Sum(e => e.Price), result);

            //Tóm tắt ví dụ: thuộc tính Object trả về một triển khai của
            //interface IDiscountHelper trong đó phương thức ApplyDiscount
            //trả về giá trị của tham số thập phân mà nó được truyền.
            //Điều này giúp ta dễ dàng để thực hiện kiểm tra đơn vị
            //bởi vì ta có thể tổng hợp giá của các đối tượng
            //sản phẩm thử nghiệm của chính mình và kiểm tra xem
            //ta có nhận được cùng một giá trị trở lại từ đối tượng
            //LinqValueCalculator hay không.
        }
Example #10
0
        public void TestMethod1()
        {
            Mock <IDiscounterHelper> mock = new Mock <IDiscounterHelper>();

            mock.Setup(m => m.ApplyDiscount(It.IsAny <decimal>())).Returns <decimal>(total => total);
            var target = new LinqValueCalculator(mock.Object);
            var goal   = products.Sum(s => s.Price);
            var result = target.ValueProducts(products);

            Assert.AreEqual(goal, result);
        }
        public void ValueProducts_TotalPrice500_Return500()
        {
            var mock = new Mock <IDiscountHelper>();

            mock.Setup(m => m.ApplyDiscount(It.IsAny <decimal>()))
            .Returns <decimal>(price => price);

            var calc = new LinqValueCalculator(mock.Object);

            Assert.AreEqual(500, calc.ValueProducts(products));
        }
Example #12
0
        public void Sum_Products_Corretly()
        {
            Mock<IDiscountHelper> mock = new Mock<IDiscountHelper>();
            mock.Setup(m => m.ApplyDiscount(It.IsAny<decimal>()))
                .Returns<decimal>(total => total);

            var target = new LinqValueCalculator(mock.Object);
            var result = target.ValueProducts(products);

            Assert.AreEqual(result, products.Sum(p => p.Price));
        }
Example #13
0
        public void SumProductsCorrectly()
        {
            Mock <IDiscountHelper> mock = new Mock <IDiscountHelper>();

            mock.Setup(m => m.ApplyDiscount(It.IsAny <decimal>()))
            .Returns <decimal>(total => total);
            var target = new LinqValueCalculator(mock.Object);
            var result = target.ValueProducts(products);

            Assert.AreEqual(products.Sum(e => e.Price), result);
        }
Example #14
0
        public void Pass_through_variable_discounts()
        {
            Mock <IDiscounterHelper> mock = new Mock <IDiscounterHelper>();

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

            var ten         = target.ValueProducts(createProduct(10));
            var hundred     = target.ValueProducts(createProduct(100));
            var fifty       = target.ValueProducts(createProduct(50));
            var fivehundrer = target.ValueProducts(createProduct(500));

            Assert.AreEqual(5, ten, "10 jest nieprawdiłowe");
            Assert.AreEqual(95, hundred, "100 jest nieprawdiłowe");
            Assert.AreEqual(45, fifty, "50 jest nieprawdiłowe");
            Assert.AreEqual(450, fivehundrer, "500 jest nieprawdiłowe");
            target.ValueProducts(createProduct(0));
        }
Example #15
0
        public void Pass_Through_Variable_Discounts()
        {
            Mock<IDiscountHelper> mock = new Mock<IDiscountHelper>();
            // Условие, если ничего другое не подходит
            mock.Setup(m => m.ApplyDiscount(It.IsAny<decimal>()))
                .Returns<decimal>(total => total);
            // Условие, если переданное значение не является положительным
            mock.Setup(m => m.ApplyDiscount(It.Is<decimal>(p => p <= 0)))
                .Throws<System.ArgumentOutOfRangeException>();
            // Условие, если переданное значение больше 100
            mock.Setup(m => m.ApplyDiscount(It.Is<decimal>(p => p > 100)))
                .Returns<decimal>(total => total * 0.9m);
            // Условие, если переданное значение входит в интервал от 10 до 100 включительно
            mock.Setup(m => m.ApplyDiscount(It.IsInRange<decimal>(10, 100, Range.Inclusive)))
                .Returns<decimal>(total => total - 5);

            var target = new LinqValueCalculator(mock.Object);

            // Создание тестируемых значений
            decimal FiveDollarsDiscount = target.ValueProducts(createProduct(5));
            decimal TenDollarsDiscount = target.ValueProducts(createProduct(10));
            decimal FiftenDollarsDiscount = target.ValueProducts(createProduct(50));
            decimal OneHandredDollarsDiscount = target.ValueProducts(createProduct(100));
            decimal TwoHandredDollarsDiscount = target.ValueProducts(createProduct(200));

            Assert.AreEqual(5, FiveDollarsDiscount, "Five dollars discount wrong");
            Assert.AreEqual(5, TenDollarsDiscount, "Five dollars discount wrong");
            Assert.AreEqual(45, FiftenDollarsDiscount, "Five dollars discount wrong");
            Assert.AreEqual(95, OneHandredDollarsDiscount, "Five dollars discount wrong");
            Assert.AreEqual(180, TwoHandredDollarsDiscount, "Five dollars discount wrong");
            target.ValueProducts(createProduct(0));
        }
Example #16
0
        public void Pass_Through_Variable_Discounts()
        {
            // Arrange
            // create and setup the mock object to mimic functionality of the MinimalDiscountHelper (as of when the test was created)
            Mock <IDiscountHelper> mock = new Mock <IDiscountHelper>();

            mock.Setup(m => m.ApplyDiscount(It.IsAny <decimal>())).Returns <decimal>(total => total);
            mock.Setup(m => m.ApplyDiscount(It.Is <decimal>(v => v == 0))).Throws <System.ArgumentOutOfRangeException>();
            mock.Setup(m => m.ApplyDiscount(It.Is <decimal>(v => v > 100))).Returns <decimal>(total => total * 0.9M);
            mock.Setup(m => m.ApplyDiscount(It.IsInRange <decimal>(10, 100, Range.Inclusive))).Returns <decimal>(total => total - 5);

            var target = new LinqValueCalculator(mock.Object);

            // Act
            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));

            // Assertion
            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));
        }
Example #17
0
        public void PassThroughVariableDiscounts()
        {
            // Организация
            var mock = new Mock <IDiscountHelper>();

            mock.Setup(helper => helper.ApplyDiscount(It.IsAny <decimal>()))
            .Returns <decimal>(total => total);
            mock.Setup(helper => helper.ApplyDiscount(It.Is <decimal>(arg => arg == 0)))
            .Throws <ArgumentOutOfRangeException>();
            mock.Setup(helper => helper.ApplyDiscount(It.Is <decimal>(arg => arg > 100)))
            .Returns <decimal>(arg => arg * 0.9M);
            mock.Setup(helper => helper.ApplyDiscount(It.IsInRange <decimal>(10, 100, Range.Inclusive)))
            .Returns <decimal>(arg => arg - 5);
            IValueCalculator target = new LinqValueCalculator(mock.Object);

            // Действие
            var fiveDollarDiscount        = target.ValueProducts(CreateProducts(5));
            var tenDollarDiscount         = target.ValueProducts(CreateProducts(10));
            var fiftyDollarDiscount       = target.ValueProducts(CreateProducts(50));
            var hundredDollarDiscount     = target.ValueProducts(CreateProducts(100));
            var fiveHundredDollarDiscount = target.ValueProducts(CreateProducts(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(CreateProducts(0));
        }
        public void Pass_Through_Variable_Discounts()
        {
            // arrange
            var discountMock = new Mock <IDiscountHelper>();

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

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

            // Assert
            Assert.AreEqual(5, fiveDollarDiscount, "$5 потеряем");
            Assert.AreEqual(5, tenDollarDiscount, "$10 потеряем");
            Assert.AreEqual(45, fiftyDollarDiscount, "$50 потеряем");
            Assert.AreEqual(95, hundredDollarDiscount, "$100 потеряем");
            Assert.AreEqual(450, fiveHundredDollarDiscount, "$500 Fail");
            target.ValueProducts(CreateProduct(0));
        }
Example #19
0
        public void PassThroughVariableDiscounts()
        {
            //arrange
            Mock <IDiscountHelper> mock = new Mock <IDiscountHelper>();

            mock.Setup(mockedObject => mockedObject.ApplyDiscount(It.IsAny <decimal>()))
            .Returns <decimal>(valuePassed => valuePassed);
            mock.Setup(mockedObject => mockedObject.ApplyDiscount(It.Is <decimal>(value => value == 0)))
            .Throws <System.ArgumentOutOfRangeException>();
            mock.Setup(mockedObject => mockedObject.ApplyDiscount(It.Is <decimal>(value => value > 100)))
            .Returns <decimal>(passedVal => (passedVal * 0.9M));
            mock.Setup(mockedObject => mockedObject.ApplyDiscount(It.IsInRange <decimal>(10, 100, Range.Inclusive)))
            .Returns <decimal>(passedVal => (passedVal - 5M));

            var target = new LinqValueCalculator(mock.Object);

            //act
            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
            Assert.AreEqual(5, fiveDollarDiscount);
            Assert.AreEqual(5, tenDollarDiscount);
            Assert.AreEqual(45, fiftyDollarDiscount);
            Assert.AreEqual(95, hundredDollarDiscount);
            Assert.AreEqual(450, fiveHundredDollarDiscount);
            target.ValueProducts(createProduct(0));
        }
Example #20
0
        [ExpectedException(typeof(ArgumentOutOfRangeException))]        // 指定计划抛出的异常
        public void TestMethod1()
        {
            Mock<IDiscountHelper> mocker = new Mock<IDiscountHelper>();     // 创建Mock对象,伪造一个IDiscountHelper的实现
            /* 装载实现的GetDiscount方法。
             * Mock的装载方式是倒序,因此要最先写最基础的场景,往下装载特殊的场景。
             */
            mocker.Setup(m => m.GetDiscount(It.IsAny<decimal>())).Returns<decimal>(total => total);     // 装载方法
            mocker.Setup(m => m.GetDiscount(It.Is<decimal>(v => v == 0))).Throws<ArgumentOutOfRangeException>();     // 参数等于0时,抛出异常
            mocker.Setup(m => m.GetDiscount(It.Is<decimal>(v => v > 100))).Returns<decimal>(total => total * 0.9M);     // 参数大于100时,返回九折
            mocker.Setup(m => m.GetDiscount(It.IsInRange<decimal>(10, 100, Range.Inclusive))).Returns<decimal>(total => total - 5);      // 参数在10与100之间,包括10和100,返回-5

            var test = new LinqValueCalculator(mocker.Object);

            //decimal zero = test.ValueProducts(InitProducts(0M));
            decimal five = test.ValueProducts(InitProducts(5M));
            decimal ten = test.ValueProducts(InitProducts(10M));
            decimal fifty = test.ValueProducts(InitProducts(50M));
            decimal hundred = test.ValueProducts(InitProducts(100M));
            decimal twoHundred = test.ValueProducts(InitProducts(200M));

            Assert.AreEqual(5M, five, "Test Five failed");
            Assert.AreEqual(5M, ten, "Test Ten failed");
            Assert.AreEqual(45M, fifty, "Test Fifty failed");
            Assert.AreEqual(95M, hundred, "Test Hundred failed");
            Assert.AreEqual(200 * 0.9M, twoHundred, "Test TwoHundred failed");
            test.ValueProducts(InitProducts(0M));
        }
Example #21
0
        public void PassThroughVariableDiscounts()
        {
            //            var discounter = new MinimumDiscountHelper();
            //            var target = new LinqValueCalculator(discounter);
            //            var goalToal = products.Sum(prod => prod.Price);

            Mock <IDiscountHelper> mock = new Mock <IDiscountHelper>();

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

            var FiveDollarDiscount        = target.ValueProducts(createProduct(5));
            var TenDollarDiscount         = target.ValueProducts(createProduct(10));
            var FiftyDollarDiscount       = target.ValueProducts(createProduct(50));
            var HundredDollarDiscount     = target.ValueProducts(createProduct(100));
            var FiveHundredDollarDiscount = target.ValueProducts(createProduct(500));

            Assert.AreEqual(5, FiveDollarDiscount, "Niepowodzenie 5 zł ");
            Assert.AreEqual(5, TenDollarDiscount, "Niepowodzenie 10 zł ");
            Assert.AreEqual(45, FiftyDollarDiscount, "Niepowodzenie 50 zł ");
            Assert.AreEqual(95, HundredDollarDiscount, "Niepowodzenie 100 zł ");
            Assert.AreEqual(450, FiveHundredDollarDiscount, "Niepowodzenie 500 zł ");
            target.ValueProducts(createProduct(0));
        }
Example #22
0
        public void Sum_Products_Correctly()
        {
            // arrange
            Mock <IDiscountHelper> mock = new Mock <IDiscountHelper>();

            mock.Setup(m => m.ApplyDiscount(It.IsAny <decimal>())).Returns <decimal>(total => total);
            LinqValueCalculator target = new LinqValueCalculator(mock.Object);

            // act
            decimal result = target.ValueProducts(this.products);

            // assert
            Assert.AreEqual(this.products.Sum(e => e.Price), result);
        }
Example #23
0
        public void Sum_Products_Correctly()
        {
            //arrange
            Mock <IDiscountHelper> mock = new Mock <IDiscountHelper>();

            mock.Setup(m => m.ApplyDiscount(It.IsAny <decimal>())).Returns <decimal>(total => total);

            var target    = new LinqValueCalculator(mock.Object);
            var goalTotal = _products.Sum(p => p.Price);
            //act
            var result = target.ValueProducts(_products);

            Assert.AreEqual(goalTotal, result);
        }
Example #24
0
        public void SumProductsCorrectly()
        {
            // Организация
            var mock = new Mock <IDiscountHelper>();

            mock.Setup(helper => helper.ApplyDiscount(It.IsAny <decimal>())).Returns <decimal>(total => total);
            IValueCalculator target = new LinqValueCalculator(mock.Object);

            // Действие
            var result = target.ValueProducts(_products);

            // Утверждение
            Assert.AreEqual(_products.Sum(product => product.Price), result);
        }
Example #25
0
        public void Sum_Products_Correctly()
        {
            // Arrange (добавляем имитированный объект)
            Mock <IDiscountHelper> mock = new Mock <IDiscountHelper>();

            mock.Setup(m => m.ApplyDiscount(It.IsAny <decimal>()))
            .Returns <decimal>(total => total);
            var target = new LinqValueCalculator(mock.Object);

            // Act
            var result = target.ValueProducts(products);

            // Assert
            Assert.AreEqual(products.Sum(e => e.Price), result);
        }
        public void ValueProductsTest()
        {
            // Arrange
            var discountMock = new Mock <IDiscountHelper>();

            discountMock.Setup(m => m.ApplyDiscount(It.IsAny <decimal>()))
            .Returns <decimal>(t => t);
            var target    = new LinqValueCalculator(discountMock.Object);
            var goalTotal = _products.Sum(e => e.Price);

            // Act
            var result = target.ValueProducts(_products);

            // Assert
            Assert.AreEqual(goalTotal, result);
        }
Example #27
0
        public void Sum_Products_Correctly()
        {
            // arrange
            Mock <IDiscountHelper> mock = new Mock <IDiscountHelper>();

            // setup - to add method to mock object
            // It - what parametres I am interested in
            mock.Setup(m => m.ApplyDiscount(It.IsAny <decimal>())).Returns <decimal>(total => total);
            var target = new LinqValueCalculator(mock.Object);

            // act
            var result = target.ValueProducts(products);

            // assert
            Assert.AreEqual(products.Sum(e => e.Price), result);
        }
Example #28
0
        public void CorrectlySumsProducts()
        {
            //arrange
            Mock <IDiscountHelper> mock = new Mock <IDiscountHelper>();

            mock.Setup(mockedClass => mockedClass.ApplyDiscount(It.IsAny <decimal>()))
            .Returns <decimal>(originalValuePassedToMockedFunction => originalValuePassedToMockedFunction);

            var target = new LinqValueCalculator(mock.Object);

            //act
            var result = target.ValueProducts(this.products);

            //assert
            Assert.AreEqual(this.products.Sum(p => p.Price), result);
        }
Example #29
0
        public void Sum_Products_Correctly()
        {
            // arrange
            Mock <IDiscountHelper> mock = new Mock <IDiscountHelper>();

            mock.Setup(m => m.ApplyDiscount(It.IsAny <decimal>())).Returns <decimal>(total => total); //  используем метод Setup, чтобы добавить метод в наш mock-объект
            // It метод в каких параметрах мы заинтересованы
            // Метод Returns позволяет определить результат, который будет возвращен
            var target = new LinqValueCalculator(mock.Object); // считывая свойство Object объекта Mock<IDiscountHelper>

            // act
            var result = target.ValueProducts(products);

            // assert
            Assert.AreEqual(products.Sum(x => x.Price), result);
        }
        public void TestComplexMockImplementation()
        {
            Mock <IDiscounter> mock = new Mock <IDiscounter>();

            mock.Setup(m => m.ApplyDiscount(It.IsAny <decimal>()))
            .Returns <decimal>(total => total);
            mock.Setup(m => m.ApplyDiscount(It.IsInRange <decimal>(10, 100, Range.Inclusive)))
            .Returns <decimal>(total => total - 5);
            mock.Setup(m => m.ApplyDiscount(It.Is <decimal>(v => v > 100)))
            .Returns <decimal>(total => total * 0.9M);
            mock.Setup(m => m.ApplyDiscount(It.Is <decimal>(v => v < 0)))
            .Throws <System.ArgumentOutOfRangeException>();

            var target = new LinqValueCalculator(mock.Object);

            var result = target.ValueProducts(m_products);

            Assert.AreEqual(m_products.Sum(e => e.Price) * 0.9M, result);
        }
Example #31
0
        public void Sum_Products_Correctly()
        {
            //arrange

            //strongly typed mock object
            Mock <IDiscountHelper> mock = new Mock <IDiscountHelper>();

            //specify the way it needs to behave
            mock.Setup(m => m.ApplyDiscount(It.IsAny <decimal>()))
            .Returns <decimal>(total => total);

            //use the mock object in the unit test
            var target = new LinqValueCalculator(mock.Object);

            //act

            var result = target.ValueProducts(products);

            //assert

            Assert.AreEqual(products.Sum(e => e.Price), result);
        }
Example #32
0
        public void Pass_Through_Variable_Discounts()
        {
            // 准备
            Mock <IDiscountHelper> mock = new Mock <IDiscountHelper>();

            // 全匹配
            mock.Setup(m => m.ApplyDiscount(It.IsAny <decimal>())).Returns <decimal>(total => total);
            // 模仿特定值,并抛出异常
            mock.Setup(m => m.ApplyDiscount(It.Is <decimal>(v => v == 0))).Throws <System.ArgumentOutOfRangeException>();
            // 模仿特定值
            mock.Setup(m => m.ApplyDiscount(It.Is <decimal>(v => v > 100))).Returns <decimal>(total => (total * 0.9M));
            // 模仿值的范围
            mock.Setup(m => m.ApplyDiscount(It.IsInRange <decimal>(10, 100, Range.Inclusive))).Returns <decimal>(total => total - 5);
            // 上面的写法等同于下面这种方式(这种方式通常更灵活):
            //mock.Setup(m => m.ApplyDiscount(It.Is<decimal>(v => v >= 10 && v <= 100))).Returns<decimal>(total => total - 5);

            // ********** 注意 ******* 注意 ********* 注意 ********* 注意 **********
            // 上述的模仿顺序是不能颠倒的,原因如下:
            // Moq 会以相反的顺序评估所给定的行为,因此会考虑调用最后一个 Setup 方法。也就是说我们在使用时必须遵循先从最一般开始向最特殊的情绪顺序就能行。
            // 即:Moq 会先去评定最后的,然后上逐步执行。按照此例,如果最普通的情况先执行,就意味着所有符合 decimal 类型参数的情形均可满足条件,那后续的如
            // 大于 100 时将不会返回预期结果,而是错误的模仿结果了。
            // ********** 注意 ******* 注意 ********* 注意 ********* 注意 **********

            var target = new LinqValueCalculator(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));
        }
Example #33
0
        public void Pass_Through_Variable_Discounts()
        {
            Mock <IDiscountHelper> mock = new Mock <IDiscountHelper>();

            mock.Setup(m => m.ApplyDiscount(It.IsAny <decimal>())).Returns <decimal>(total => total);
            mock.Setup(m => m.ApplyDiscount(It.Is <decimal>(v => v == 0))).Throws <System.ArgumentOutOfRangeException>();
            mock.Setup(m => m.ApplyDiscount(It.Is <decimal>(v => v > 100))).Returns <decimal>(total => (total * 0.9M));
            mock.Setup(m => m.ApplyDiscount(It.IsInRange <decimal>(10, 100, Range.Inclusive))).Returns <decimal>(total => (total - 5));

            var target = new LinqValueCalculator(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, "Niepowodzenie 5 zł");
            Assert.AreEqual(5, TenDollarDiscount, "Niepowodzenie 10 zł");
            Assert.AreEqual(45, FiftyDollarDiscount, "Niepowodzenie 50 zł");
            Assert.AreEqual(95, HundredDollarDiscount, "Niepowodzenie 100 zł");
            Assert.AreEqual(450, FiveHundredDollarDiscount, "Niepowodzenie 500 zł");
            target.ValueProducts(createProduct(0));
        }