public void TestSetup()
        {
            // Prepare here our fake dependencies.
            var account1 = new Account { Id = 1, Name = "Account1", Balance = 100.5m };
            var account2 = new Account { Id = 2, Name = "Account2", Balance = 2000.17m };

            var order1 = new Order
            {
                Id = 1,
                AccountId = 1,
                Account = account1,
                Date = DateTime.Now,
                Total = PurchaseService.ORDERS_HISTORY_DISCOUNT_THRESHOLD - 10
            };
            var order2 = new Order { Id = 2, AccountId = 1, Account = account1, Date = DateTime.Now, Total = 20.1m };
            var order3 = new Order { Id = 3, AccountId = 2, Account = account2, Date = DateTime.Now, Total = 170.87m };

            var product1 = new Product { Id = 1, Title = "Product1", Price = 250.99m, Amount = 2 };
            var product2 = new Product { Id = 2, Title = "Product2", Price = 50.5m, Amount = 1 };
            var product3 = new Product { Id = 3, Title = "Product3", Price = 70.15m, Amount = 10 };
            var product4 = new Product { Id = 4, Title = "Product4", Price = 10.75m, Amount = 0 };

            _fakeAccountRepository.Data.AddRange(new[] { account1, account2 });
            _fakeOrderRepository.Data.AddRange(new[] { order1, order2, order3 });
            _fakeProductRepository.Data.AddRange(new[] { product1, product2, product3, product4 });
        }
 public ProductViewModel(Product product)
 {
     Id = product.Id;
     Title = product.Title;
     Price = product.Price;
     _amount = product.Amount;
 }
        public void Purchase_When_purchase_products_Then_products_amount_correctly_updated()
        {
            // Arrange
            var product1 = new Product { Id = 1, Price = 1, Amount = 5 };
            var product2 = new Product { Id = 2, Price = 2, Amount = 10 };
            var product3 = new Product { Id = 3, Price = 3, Amount = 7 };

            var productRepository = A.Fake<IRepository<Product>>();
            A.CallTo(() => productRepository.Query()).Returns(new[] { product1, product2, product3 }.AsQueryable());

            var account = new Account { Id = 1, Balance = 10 };

            var accountRepository = A.Fake<IRepository<Account>>();
            A.CallTo(() => accountRepository.Get(A<int>._)).Returns(account);

            var unitOfWork = A.Fake<IUnitOfWork>();
            A.CallTo(() => unitOfWork.Repository<Product>()).Returns(productRepository);
            A.CallTo(() => unitOfWork.Repository<Account>()).Returns(accountRepository);

            var unitOfWorkFactory = A.Fake<IUnitOfWorkFactory>();
            A.CallTo(() => unitOfWorkFactory.Create()).Returns(unitOfWork);

            var service = new PurchaseService(unitOfWorkFactory);

            // Act
            service.Purchase(account.Id, new[] { product1.Id, product2.Id }, product1.Price + product2.Price);

            // Assert
            Assert.That(product1.Amount, Is.EqualTo(4));
            Assert.That(product2.Amount, Is.EqualTo(9));
        }
        public void Purchase_When_purchase_products_with_amount_0_Then_throw_argument_exception()
        {
            // Arrange
            var product1 = new Product { Id = 1, Price = 1, Amount = 0 };

            var productRepository = A.Fake<IRepository<Product>>();
            A.CallTo(() => productRepository.Query()).Returns(new[] { product1 }.AsQueryable());

            var account = new Account { Id = 1, Balance = 1 };

            var accountRepository = A.Fake<IRepository<Account>>();
            A.CallTo(() => accountRepository.Get(A<int>._)).Returns(account);

            var unitOfWork = A.Fake<IUnitOfWork>();
            A.CallTo(() => unitOfWork.Repository<Product>()).Returns(productRepository);
            A.CallTo(() => unitOfWork.Repository<Account>()).Returns(accountRepository);

            var unitOfWorkFactory = A.Fake<IUnitOfWorkFactory>();
            A.CallTo(() => unitOfWorkFactory.Create()).Returns(unitOfWork);

            var service = new PurchaseService(unitOfWorkFactory);

            // Act and Assert
            Assert.Throws<ArgumentException>(() => service.Purchase(account.Id, new[] { product1.Id }, product1.Price));
        }
        public void Purchase_When_purchase_products_Then_SaveChanges_of_UnitOfWork_called()
        {
            // Arrange
            var product1 = new Product { Id = 1, Price = 1, Amount = 1 };

            var productRepository = A.Fake<IRepository<Product>>();
            A.CallTo(() => productRepository.Query()).Returns(new[] { product1 }.AsQueryable());

            var account = new Account { Id = 1, Balance = 1 };

            var accountRepository = A.Fake<IRepository<Account>>();
            A.CallTo(() => accountRepository.Get(A<int>._)).Returns(account);

            var unitOfWork = A.Fake<IUnitOfWork>();
            A.CallTo(() => unitOfWork.Repository<Product>()).Returns(productRepository);
            A.CallTo(() => unitOfWork.Repository<Account>()).Returns(accountRepository);

            var unitOfWorkFactory = A.Fake<IUnitOfWorkFactory>();
            A.CallTo(() => unitOfWorkFactory.Create()).Returns(unitOfWork);

            var service = new PurchaseService(unitOfWorkFactory);

            // Act
            service.Purchase(account.Id, new[] { product1.Id }, product1.Price);

            // Assert
            A.CallTo(() => unitOfWork.SaveChanges()).MustHaveHappened(Repeated.Exactly.Once);
        }
        public void Purchase_When_trying_to_purchase_product_with_zero_amount_Then_throw_purchase_exception()
        {
            var product1 = new Product { Id = 1, Amount = 0, Price = 100m };
            var account = new Account { Id = 1, Balance = 105m };

            var accountRepository = A.Fake<IRepository<Account>>();
            A.CallTo(() => accountRepository.Get(A<int>._)).Returns(account);

            var productRepository = A.Fake<IRepository<Product>>();
            A.CallTo(() => productRepository.Query()).Returns(new[] { product1 }.AsQueryable());

            var unitOfWork = A.Fake<IUnitOfWork>();
            A.CallTo(() => unitOfWork.Repository<Account>()).Returns(accountRepository);
            A.CallTo(() => unitOfWork.Repository<Product>()).Returns(productRepository);

            var unitOfWorkFactory = A.Fake<IUnitOfWorkFactory>();
            A.CallTo(() => unitOfWorkFactory.Create()).Returns(unitOfWork);

            var service = new PurchaseService(unitOfWorkFactory);

            service.Purchase(account.Id, new[] { product1.Id }, product1.Price);
        }
        public void Purchase_When_purchase_made_Then_account_balance_updates_properly()
        {
            var product1 = new Product { Id = 1, Amount = 1, Price = 100m };
            var account = new Account { Id = 1, Balance = 105m };

            var accountRepository = A.Fake<IRepository<Account>>();
            A.CallTo(() => accountRepository.Get(A<int>._)).Returns(account);

            var productRepository = A.Fake<IRepository<Product>>();
            A.CallTo(() => productRepository.Query()).Returns(new[] { product1 }.AsQueryable());

            var unitOfWork = A.Fake<IUnitOfWork>();
            A.CallTo(() => unitOfWork.Repository<Account>()).Returns(accountRepository);
            A.CallTo(() => unitOfWork.Repository<Product>()).Returns(productRepository);

            var unitOfWorkFactory = A.Fake<IUnitOfWorkFactory>();
            A.CallTo(() => unitOfWorkFactory.Create()).Returns(unitOfWork);

            var service = new PurchaseService(unitOfWorkFactory);

            service.Purchase(account.Id, new[] { product1.Id }, product1.Price);

            Assert.That(account.Balance, Is.EqualTo(5));
        }
        public void Purchase_When_amount_of_product_equals_zero_Then_throw_exception()
        {
            // Arrange
            var product1 = new Product { Id = 1, Price = 1, Amount = 5 };
            var product2 = new Product { Id = 2, Price = 2, Amount = 0 };

            var productRepository = A.Fake<IRepository<Product>>();
            A.CallTo(() => productRepository.Query()).Returns(new[] { product1, product2}.AsQueryable());

            var account = new Account { Id = 1, Balance = 10};

            var accountRepository = A.Fake<IRepository<Account>>();
            A.CallTo(() => accountRepository.Get(A<int>._)).Returns(account);

            var unitOfWork = A.Fake<IUnitOfWork>();
            A.CallTo(() => unitOfWork.Repository<Product>()).Returns(productRepository);
            A.CallTo(() => unitOfWork.Repository<Account>()).Returns(accountRepository);

            var unitOfWorkFactory = A.Fake<IUnitOfWorkFactory>();
            A.CallTo(() => unitOfWorkFactory.Create()).Returns(unitOfWork);

            var service = new PurchaseService(unitOfWorkFactory);

            // Act and Assert
            Assert.Throws<PurchaseException>(() => service.Purchase(account.Id, new[] { product1.Id, product2.Id, }, product1.Price + product2.Price));
        }
 public void GivenIHaveCreatedAProductWithPrice(int p0)
 {
     _product = new Product { Id = 1, Price = p0, Amount = 5 };
 }
        public void Purchase_When_product_amount_is_0_Then_throw_an_exception()
        {
            // Arrange
            var product = new Product { Id = 1, Title = "Test product", Price = 10, Amount = 0 };

            var productRepository = A.Fake<IRepository<Product>>();
            A.CallTo(() => productRepository.Query()).Returns(new[] { product }.AsQueryable());

            var account = new Account { Id = 1, Balance = 1000m };

            var accountRepository = A.Fake<IRepository<Account>>();
            A.CallTo(() => accountRepository.Get(A<int>._)).Returns(account);

            var unitOfWork = A.Fake<IUnitOfWork>();
            A.CallTo(() => unitOfWork.Repository<Product>()).Returns(productRepository);
            A.CallTo(() => unitOfWork.Repository<Account>()).Returns(accountRepository);

            var unitOfWorkFactory = A.Fake<IUnitOfWorkFactory>();
            A.CallTo(() => unitOfWorkFactory.Create()).Returns(unitOfWork);

            var service = new PurchaseService(unitOfWorkFactory);

            // Assert
            Assert.Throws<PurchaseException>(() => service.Purchase(account.Id, new[] { product.Id }, product.Price));
        }
        public void Purchase_When_purchase_products_with_0_amount_Then_purchase_should_throw_an_exception()
        {
            // Arrange
            var product1 = new Product { Id = 1, Price = 2, Amount = 0, Title = "Keyboard" };
            var product2 = new Product { Id = 2, Amount = 2, Price = 2, Title = "Mouse" };
            var productRepository = A.Fake<IRepository<Product>>();
            A.CallTo(() => productRepository.Query()).Returns(new[] { product1 }.AsQueryable());

            var account = new Account { Id = 1, Balance = 5 };

            var accountRepository = A.Fake<IRepository<Account>>();
            A.CallTo(() => accountRepository.Get(A<int>._)).Returns(account);

            var unitOfWork = A.Fake<IUnitOfWork>();
            A.CallTo(() => unitOfWork.Repository<Product>()).Returns(productRepository);
            A.CallTo(() => unitOfWork.Repository<Account>()).Returns(accountRepository);

            var unitOfWorkFactory = A.Fake<IUnitOfWorkFactory>();
            A.CallTo(() => unitOfWorkFactory.Create()).Returns(unitOfWork);

            var service = new PurchaseService(unitOfWorkFactory);

            // Assert
            Assert.Throws<PurchaseException>(() => service.Purchase(account.Id, new[] { product1.Id, product2.Id }, product1.Price + product2.Price));
        }
 public void GivenIBuyTwoProductsWhichAreWorthAnd(decimal price1, decimal price2)
 {
     _product1 = new Product { Id = 1, Price = price1, Amount = 1 };
     _product2 = new Product { Id = 2, Price = price2, Amount = 1 };
 }
 public void GivenIAmGoingToByProductAmountIs()
 {
     _product = new Product { Id = 1, Amount = 0, Price = 1, Title = "Test product" };
 }
 public void GivenIAmGoingToByProductWhichPriceIs(int p0)
 {
     _product = new Product { Id = 1, Amount = 1, Price = p0, Title = "Test product" };
 }