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