Exemplo n.º 1
0
        public void Initalize()
        {
            _factory = new MockRepository(MockBehavior.Loose);
            _container = new AutoMockContainer(_factory);
            _coins = new List<Coin>();
            _purchaseServiceMock = _container.GetMock<IPurchaseService>();

            var product_a = new Product() { Price = 1, Title = "Apple" };
            var product_b = new Product() { Price = .75m, Title = "Banana" };

            var slot_a = new Slot() { Button = "A", Product = product_a, MaxNumberOfProduct = 5, Quantity = 4 };
            var slot_b = new Slot() { Button = "B", Product = product_b, MaxNumberOfProduct = 5, Quantity = 0 };

            _dollar = new Coin() { Title = "One Dollar", Value = 1, ShortName = "O" };
            _quarter = new Coin() { Title = "Quarter", Value = .25m, ShortName = "Q" };
            _dime = new Coin() { Title = "Dime", Value = .1m, ShortName = "d" };
            _nickle = new Coin() { Title = "Nickle", Value = .05m, ShortName = "n" };
            _penny = new Coin() { Title = "Penny", Value = .01m, ShortName = "p" };

            _slots = new List<Slot>() { slot_a, slot_b };

            _maxSlots = 5;

            _machineService = new MachineService(_maxSlots, _purchaseServiceMock.Object);
        }
Exemplo n.º 2
0
 public Product Deliver(Slot slot)
 {
     var retval = new Product();
     if (slot.Quantity > 0)
     {
         slot.Quantity = slot.Quantity - 1;
         retval = slot.Product;
     }
     return retval;
 }
Exemplo n.º 3
0
 public string CreateSlot(string slotLetter, int maxNumberOfProduct, int numberOfProducts, Product product)
 {
     var msg = "No room to add more products";
     if (_machine.Slots.Count < _machine.MaxSlots)
     {
         var slot = new Slot() {Button = slotLetter, MaxNumberOfProduct = maxNumberOfProduct, Product = product, Quantity = numberOfProducts};
         _machine.Slots.Add(slot);
         msg = string.Format("Your products were added to the slot: {0}", slot.Button);
     }
     return msg;
 }
Exemplo n.º 4
0
        public void PurchaseProductSlotEmptyTestSuccess()
        {
            string slotLetter = "A";
            var product_b = new Product() { Price = .75m, Title = "Apple" };
            _coins.Add(_dollar);
            _coins.Add(_dime);

            _purchaseServiceMock.Setup(s => s.Purchase(It.IsAny<Slot>(), It.IsAny<ICollection<Coin>>())).Returns(_coins.Sum(s => s.Value - product_b.Price)).Verifiable("Purchase did not run");
            _purchaseServiceMock.Setup(s => s.MakeChange(It.IsAny<ICollection<Coin>>(), It.IsAny<decimal>()))
                .Returns(new List<Coin>()).Verifiable();

            _machineService.CreateSlot(slotLetter, 10, 0, product_b);
            var results = _machineService.Purchase(slotLetter);

            Assert.IsTrue(results.Count == 0);
        }
Exemplo n.º 5
0
        public void PurchaseProductTestSuccess()
        {
            string slotLetter = "A";
            var product_a = new Product() { Price = 1, Title = "Apple" };
            _coins.Add(_dollar);
            _coins.Add(_dime);
            var change = new List<Coin>() {_dime};
            _purchaseServiceMock.Setup(s => s.Purchase(It.IsAny<Slot>(), It.IsAny<ICollection<Coin>>())).Returns(_coins.Sum(s => s.Value - product_a.Price)).Verifiable("Purchase did not run");
            _purchaseServiceMock.Setup(s => s.MakeChange(It.IsAny<ICollection<Coin>>(), It.IsAny<decimal>()))
                .Returns(change).Verifiable();
            //run code
            _machineService.CreateSlot(slotLetter, 10, 10, product_a);

            var results = _machineService.Purchase(slotLetter);

            Assert.IsTrue(results != null);
            var total = results.Sum(s => s.Value);
            var expectedResult = Convert.ToDecimal(_coins.Sum(s => s.Value) - product_a.Price);

            Assert.IsTrue(Convert.ToDecimal(total) == expectedResult );
        }