Beispiel #1
0
        public void Balance_When_purchase_products_Then_balance_is_correctly_updated()
        {
            // Arrange
            var balance1 = new Account { Id = 1,  Name = "YYY", Balance = 100 };
            var balance2 = new Account { Id = 2, Name = "YY", Balance = 10 };
            var balance3 = new Account { Id = 3, Name = "Y", Balance = 101 };

            var productBalance = A.Fake<IRepository<Account>>();
            A.CallTo(() => productBalance.Query()).Returns(new[] { balance1, balance2, balance3 }.AsQueryable());

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

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

            var unitOfWork = A.Fake<IUnitOfWork>();
            A.CallTo(() => unitOfWork.Repository<Account>()).Returns(productBalance);
            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[] { balance1.Id, balance2.Id }, balance1.Balance + balance2.Balance);

            // Assert
            Assert.That(balance1.Balance, Is.EqualTo(102));
            Assert.That(balance2.Balance, Is.EqualTo(11));
        }
        public void Purchase_When_not_enough_balance_Then_throw_exception()
        {
            // Assert
            var service = new PurchaseService(_fakeUnitOfWorkFactory);

            // Act and Assert
            Assert.Throws<PurchaseException>(() => service.Purchase(1, Enumerable.Empty<int>(), 200.50m));
        }
        public void WhenIByProducts()
        {
            var productRepository = A.Fake<IRepository<Product>>();
            A.CallTo(() => productRepository.Query()).Returns(new[] { _product }.AsQueryable());

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

            service.Purchase(_account.Id, new[] { _product.Id }, _product.Price);
        }
        public void WhenIAskHowMuchIsTheBalanceNow()
        {
            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, _product2 }.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, _product2.Id }, _product1.Price+_product2.Price);
            _result = _account.Balance;
        }
        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_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_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 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 Purchase_When_not_selected_any_product_Then_throw_exception()
        {
            // Assert
            var service = new PurchaseService(_fakeUnitOfWorkFactory);

            // Act and Assert
            Assert.Throws<PurchaseException>(() => service.Purchase(2, Enumerable.Empty<int>(), 200.50m));
        }