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 PercentageDiscountStrategyShouldRunIsCorrect(
     Product testProduct,
     PercentageDiscountStrategy sut)
 {
     testProduct.DiscountType = "percentage";
     Assert.True(sut.ShouldRunFor(testProduct));
 }
Exemplo n.º 3
0
 public void PercentageDiscountStrategyShouldNotRunIsCorrect(
     Product testProduct,
     string bogusdiscounttype,
     PercentageDiscountStrategy sut)
 {
     testProduct.DiscountType = bogusdiscounttype;
     Assert.False(sut.ShouldRunFor(testProduct));
 }
Exemplo n.º 4
0
 public void PercentageDiscountStrategyThrowsOnLowDiscount(
     Product p,
     decimal discount,
     PercentageDiscountStrategy sut)
 {
     if (Math.Abs(discount) != 0)
     {
         discount = -Math.Abs(discount);
     }
     Assert.Throws <InvalidDiscountException>(() => sut.DiscountProduct(p, discount));
 }
Exemplo n.º 5
0
        public void PercentageDiscountStrategyCalculationIsCorrect(
            Product p,
            PercentageDiscountStrategy sut)
        {
            var discount = (decimal) new Random().NextDouble();

            Assert.True(discount > 0);
            var actual = sut.DiscountProduct(p, discount);

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