示例#1
0
        public void GivenNotExistedDestinationAccountId_WhenTransfer_ThrowKeyNotFoundException()
        {
            //arrange
            _accountRepoMock.Setup(x => x.ReadOne(It.IsAny <Expression <Func <Account, bool> > >()))
            .Returns((Expression <Func <Account, bool> > expression) =>
            {
                var result = AccountFakeDb.SingleOrDefault(expression);
                return(result);
            })
            .Verifiable();

            _accountRepoMock.Setup(x => x.CommitChanges()).Verifiable();

            _transactionRepoMock.Setup(a => a.Create(It.IsAny <TransactionHistory>())).Verifiable();

            //action
            KeyNotFoundException ex = Assert.Throws <KeyNotFoundException>(() => _accountService.TransferMoney(1, 10, 200));

            //assert
            Assert.NotNull(ex);
            Assert.NotEmpty(ex.Message);
            _accountRepoMock.Verify(a => a.CommitChanges(), Times.Never);
            _transactionRepoMock.Verify(a => a.Create(It.IsAny <TransactionHistory>()), Times.Never);
            _accountRepoMock.Verify(a => a.ReadOne(It.IsAny <Expression <Func <Account, bool> > >()), Times.Exactly(2));
        }
示例#2
0
        public void GivenNotEnoughMoney_WhenTransfer_ThrowException()
        {
            //arrange
            Account account = new Account()
            {
                Id = 1, UserId = "1", Balance = 0
            };

            _accountRepoMock.Setup(x => x.ReadOne(It.IsAny <Expression <Func <Account, bool> > >()))
            .Returns(account)
            .Verifiable();

            _accountRepoMock.Setup(x => x.Read(It.IsAny <Expression <Func <Account, bool> > >()))
            .Returns((Expression <Func <Account, bool> > expression) =>
            {
                var result = AccountFakeDb.Where(expression);
                return(result);
            })
            .Verifiable();
            _accountRepoMock.Setup(x => x.CommitChanges()).Verifiable();

            _transactionRepoMock.Setup(a => a.Create(It.IsAny <TransactionHistory>())).Verifiable();

            //action
            Exception ex = Assert.Throws <Exception>(() => _accountService.TransferMoney(1, 1000, 2));

            //assert
            Assert.NotNull(ex);
            Assert.NotEmpty(ex.Message);
            Assert.Equal(0, account.Balance);
            _accountRepoMock.Verify(a => a.CommitChanges(), Times.Never);
            _transactionRepoMock.Verify(a => a.Create(It.IsAny <TransactionHistory>()), Times.Never);
            _accountRepoMock.Verify(a => a.ReadOne(It.IsAny <Expression <Func <Account, bool> > >()), Times.Exactly(2));
        }
示例#3
0
        public async Task GivenCorrectData_WhenTransfer_ShouldThrowConcurrencyErrorOrInconclusive(int accountId, double value, int desAccountId)
        {
            //arrange
            Exception result        = null;
            string    errorMessage  = "Data was updated please reload the data";
            var       sourceAccount = AccountFakeDb.SingleOrDefault(a => a.Id == accountId);
            var       desAccount    = AccountFakeDb.SingleOrDefault(a => a.Id == desAccountId);

            byte[] rowVersionAccount = sourceAccount.RowVersion, rowVersionDesAccount = desAccount.RowVersion;


            _accountRepoMock.Setup(x => x.ReadOne(It.IsAny <Expression <Func <Account, bool> > >()))
            .Returns((Expression <Func <Account, bool> > expression) =>
            {
                var data = AccountFakeDb.SingleOrDefault(expression);
                return(data);
            })
            .Verifiable();

            _accountRepoMock.Setup(a => a.Update(It.IsAny <Account>()))
            .Returns(1);

            _accountRepoMock.Setup(x => x.CommitChanges()).Returns(() =>
            {
                if (AccountFakeDb.Count(a => a.RowVersion == rowVersionAccount ||
                                        a.RowVersion == rowVersionDesAccount) < 2)
                {
                    //Not throwing this because the concurrency problem sometimes happend
                    //and xUnit doesn't support Inconclusive result
                    //So that why I new a exception instead of throw DbUpdateConcurrencyException
                    result = new Exception(errorMessage);
                    return(0);
                }

                sourceAccount.RowVersion = BitConverter.GetBytes(DateTime.Now.Ticks);
                desAccount.RowVersion    = BitConverter.GetBytes(DateTime.Now.Ticks);
                Skip.If(result == null);
                return(1);
            }).Verifiable();

            _transactionRepoMock.Setup(a => a.Create(It.IsAny <TransactionHistory>())).Verifiable();
            _transactionRepoMock.Setup(a => a.CommitChanges()).Returns(1).Verifiable();

            //action
            await Task <bool> .Factory.StartNew(() => _accountService.TransferMoney(accountId, value, desAccountId));

            if (result != null)
            {
                //assert
                Assert.True(result.Message == errorMessage);
            }

            //verify
            _accountRepoMock.Verify(a => a.CommitChanges(), Times.Once);
            _accountRepoMock.Verify(a => a.ReadOne(It.IsAny <Expression <Func <Account, bool> > >()), Times.Exactly(2));
        }
示例#4
0
        public async Task GivenValidData_WhenUpdateBalance_ShouldThrowConcurrencyErrorOrInconclusive(int accountId, double value, string userId)
        {
            //arrange
            Exception result       = null;
            string    errorMessage = "Data was updated please reload the data";
            var       account      = AccountFakeDb.SingleOrDefault(a => a.Id == accountId);

            byte[] rowVersion = account.RowVersion;

            _accountRepoMock.Setup(x => x.ReadOne(It.IsAny <Expression <Func <Account, bool> > >()))
            .Returns((Expression <Func <Account, bool> > expression) =>
            {
                var data = AccountFakeDb.SingleOrDefault(expression);
                return(data);
            })
            .Verifiable();

            _accountRepoMock.Setup(a => a.CommitChanges())
            .Returns(() =>
            {
                if (AccountFakeDb.SingleOrDefault(a => a.Id == accountId && a.RowVersion == rowVersion) == null)
                {
                    //Not throwing this because the concurrency problem sometimes happend
                    //and xUnit doesn't support Inconclusive result
                    //So that why I new a exception instead of throw DbUpdateConcurrencyException
                    result = new Exception(errorMessage);
                    return(0);
                }
                account.RowVersion = BitConverter.GetBytes(DateTime.Now.Ticks);

                Skip.If(result == null);
                return(1);
            });

            _transactionRepoMock.Setup(a => a.Create(It.IsAny <TransactionHistory>()))
            .Verifiable("Creation of transaction should be call at least once");
            _transactionRepoMock.Setup(a => a.CommitChanges())
            .Verifiable("Saving changes transaction should be call at least once");
            // Action
            await Task <bool> .Factory.StartNew(() => _accountService.UpdateBalance(value, accountId, userId));

            //assert
            if (result != null)
            {
                Assert.True(result.Message == errorMessage);
            }

            _accountRepoMock.Verify(a => a.ReadOne(It.IsAny <Expression <Func <Account, bool> > >()), Times.Once);
            _transactionRepoMock.Verify(a => a.Create(It.IsAny <TransactionHistory>()), Times.Never);
            _transactionRepoMock.Verify(a => a.CommitChanges(), Times.Never);
        }
示例#5
0
        public void GivenAccountNotExisted_WhenReadAccountByNumber_ThrowArgumentNullException(string accountNumber)
        {
            //arrange
            _accountRepoMock.Setup(a => a.ReadOne(It.IsAny <Expression <Func <Account, bool> > >()))
            .Returns((Expression <Func <Account, bool> > expression) =>
            {
                var data = AccountFakeDb.SingleOrDefault(expression);
                return(data);
            });
            //Action
            KeyNotFoundException result = Assert.Throws <KeyNotFoundException>(() => _accountService.ReadOneAccountByNumber(accountNumber));

            //assert
            Assert.NotNull(result);
        }
示例#6
0
        public void GivenUnexistedAccount_WhenCheckExistedAccount_False(int?accountId, string userId, string password = "")
        {
            //arrange
            _accountRepoMock.Setup(a => a.Read(It.IsAny <Expression <Func <Account, bool> > >())).
            Returns((Expression <Func <Account, bool> > expression) =>
            {
                var data = AccountFakeDb.Where(expression);
                return(data);
            });
            //Action

            var result = _accountService.IsAccountExisted(accountId, userId, password);

            //assert
            Assert.Equal(false, result);
        }
示例#7
0
        public void GivenValidData_WhenReadAccountByNumber_Account(string accountNumber)
        {
            //arrange
            _accountRepoMock.Setup(a => a.ReadOne(It.IsAny <Expression <Func <Account, bool> > >()))
            .Returns((Expression <Func <Account, bool> > expression) =>
            {
                var data = AccountFakeDb.SingleOrDefault(expression);
                return(data);
            });
            //Action
            var result = _accountService.ReadOneAccountByNumber(accountNumber);

            //assert
            Assert.NotNull(result);
            Assert.True(result.AccountNumber == accountNumber);
        }
示例#8
0
        public void GivenExistedAccountWithoutPassword_WhenCheckExistedAccount_True(int?accountId, string userId, string password = "")
        {
            //arrange
            Account foundEntity = new Account();

            _accountRepoMock.Setup(a => a.Read(It.IsAny <Expression <Func <Account, bool> > >())).
            Returns((Expression <Func <Account, bool> > exp) =>
            {
                var data    = AccountFakeDb.Where(exp);
                foundEntity = AccountFakeDb.First(exp);
                return(data);
            });
            //Action
            var result = _accountService.IsAccountExisted(accountId, userId, password);

            //assert
            Assert.Equal(true, result);
            Assert.True(foundEntity.Id == accountId &&
                        foundEntity.UserId == userId &&
                        foundEntity.Password != password);
        }