Ejemplo n.º 1
0
        public void CanTransferFunds()
        {
            var data = new List <Account>
            {
                new Account {
                    Id = 1, Name = "Account 1", Funds = 500, AccountType = AccountTypeEnum.IndividualInvestment
                },
                new Account {
                    Id = 2, Name = "Account 2", Funds = 750, AccountType = AccountTypeEnum.Checking
                },
            }.AsQueryable();

            var mockSet = new Mock <DbSet <Account> >();

            mockSet.As <IQueryable <Account> >().Setup(m => m.Provider).Returns(data.Provider);
            mockSet.As <IQueryable <Account> >().Setup(m => m.Expression).Returns(data.Expression);
            mockSet.As <IQueryable <Account> >().Setup(m => m.ElementType).Returns(data.ElementType);
            mockSet.As <IQueryable <Account> >().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());

            var mockContext = new Mock <BankingDataContext>();

            mockContext.Setup(m => m.Accounts).Returns(mockSet.Object);

            var service = new BankingTransactionService(mockContext.Object);

            Account account1 = service.GetAccountById(1);
            Account account2 = service.GetAccountById(2);

            //check that ef returns correct starting data
            Assert.AreEqual(account1.Funds, 500);
            Assert.AreEqual(account2.Funds, 750);
            //transfer $100 from 1 to 2
            service.Transfer(1, 2, 100);
            Assert.AreEqual(account1.Funds, 400);
            Assert.AreEqual(account2.Funds, 850);
            //transfer $200 from 2 to 1
            service.Transfer(2, 1, 200);
            Assert.AreEqual(account1.Funds, 600);
            Assert.AreEqual(account2.Funds, 650);
        }
Ejemplo n.º 2
0
        public void CannotTransferMoreThanAccountHas()
        {
            var data = new List <Account>
            {
                new Account {
                    Id = 1, Name = "Account 1", Funds = 500, AccountType = AccountTypeEnum.IndividualInvestment
                },
                new Account {
                    Id = 2, Name = "Account 2", Funds = 750, AccountType = AccountTypeEnum.Checking
                },
            }.AsQueryable();

            var mockSet = new Mock <DbSet <Account> >();

            mockSet.As <IQueryable <Account> >().Setup(m => m.Provider).Returns(data.Provider);
            mockSet.As <IQueryable <Account> >().Setup(m => m.Expression).Returns(data.Expression);
            mockSet.As <IQueryable <Account> >().Setup(m => m.ElementType).Returns(data.ElementType);
            mockSet.As <IQueryable <Account> >().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());

            var mockContext = new Mock <BankingDataContext>();

            mockContext.Setup(m => m.Accounts).Returns(mockSet.Object);

            var service = new BankingTransactionService(mockContext.Object);

            Account account1 = service.GetAccountById(1);
            Account account2 = service.GetAccountById(2);

            //check that ef returns correct starting data
            Assert.AreEqual(account1.Funds, 500);
            Assert.AreEqual(account2.Funds, 750);

            //test that method fails with exception when trying to transfer 1200 from 1 to 2
            try
            {
                service.Transfer(1, 2, 1200);
                Assert.Fail("An exception should have been thrown");                 //if this runs an exception was not thrown
            }
            catch (Exception e)
            {
                //check that account funds weren't changed
                Assert.AreEqual(account1.Funds, 500);
                Assert.AreEqual(account2.Funds, 750);
                //check that this operation threw the expected exception
                Assert.IsInstanceOfType(e, typeof(WithdrawalExceedsAvailableFundsException));
            }
        }