Exemplo n.º 1
0
        public void CharacterizeRefactoredVersion(
            Product dummyProduct,
            Mock <IDiscountRepository> dummyRepository,
            Mock <IAuthorize> dummyAuthorizer)
        {
            dummyProduct.DiscountType = "percentage";
            dummyRepository.Setup(x => x.GetDiscountForType(
                                      It.IsAny <string>(),
                                      It.IsAny <string>())).Returns(.5M);
            dummyAuthorizer.Setup(x => x.IsAuthorized()).Returns(true);

            // composition root
            var percentoff = new PercentageDiscountStrategy();
            var moneyoff   = new MoneyOffDiscountStrategy();
            var unknown    = new UnknownDiscountStrategy();

            var runfirst = new RunFirstCompositeStrategy(new IDiscountStrategy[]
            {
                percentoff,
                moneyoff,
                unknown
            });
            var discountCalculator = new RefactoredProductClass(
                dummyRepository.Object,
                runfirst);
            var authorizedSut = new AuthorizableDiscountCalculator(
                dummyAuthorizer.Object,
                discountCalculator);
            // end composition root

            var actual = authorizedSut.GetDiscountPrice(dummyProduct);

            Assert.Equal(dummyProduct.Price * .5M, actual);
        }
Exemplo n.º 2
0
 public void MoneyOffDiscountStrategyShouldRunIsCorrect(
     Product testProduct,
     MoneyOffDiscountStrategy sut)
 {
     testProduct.DiscountType = "moneyoff";
     Assert.True(sut.ShouldRunFor(testProduct));
 }
Exemplo n.º 3
0
 public void MoneyOffDiscountStrategyShouldNotRunIsCorrect(
     Product testProduct,
     string bogusdiscounttype,
     MoneyOffDiscountStrategy sut)
 {
     testProduct.DiscountType = bogusdiscounttype;
     Assert.False(sut.ShouldRunFor(testProduct));
 }
Exemplo n.º 4
0
 public void MoneyOffDiscountStrategyThrowsOnLowDiscount(
     Product p,
     decimal discount,
     MoneyOffDiscountStrategy sut)
 {
     if (Math.Abs(discount) != 0)
     {
         discount = -discount;
     }
     Assert.Throws <InvalidDiscountException>(() => sut.DiscountProduct(p, discount));
 }
Exemplo n.º 5
0
 public void MoneyOffDiscountStrategyThrowsOnHighDiscount(
     Product p,
     decimal discount,
     MoneyOffDiscountStrategy sut)
 {
     while (discount < p.Price)
     {
         discount = discount + Math.Abs(discount);
     }
     Assert.Throws <InvalidDiscountException>(() => sut.DiscountProduct(p, discount));
 }
Exemplo n.º 6
0
        public void MoneyOffDiscountStrategyCalculationIsCorrect(
            Product p,
            decimal discount,
            MoneyOffDiscountStrategy sut)
        {
            if (discount > p.Price)
            {
                discount = discount - (discount - 1);
            }
            var actual = sut.DiscountProduct(p, discount);

            Assert.Equal(p.Price - discount, actual);
        }