public void TransferMoney_ShouldAdjustBalancesAndSaveTheChanges() { //Arrange decimal originalFromBalance = Random.Next(500, 1001); decimal originalToBalance = Random.Next(0, 1001); Account fromAccount = new AccountBuilder().WithBalance(originalFromBalance).Build(); Account toAccount = new AccountBuilder().WithBalance(originalToBalance).Build(); _accountRepositoryMock.Setup(repo => repo.GetByAccountNumber(fromAccount.AccountNumber)).Returns(fromAccount); _accountRepositoryMock.Setup(repo => repo.GetByAccountNumber(toAccount.AccountNumber)).Returns(toAccount); decimal amount = Random.Next(10, 101); //Act Result result = _service.TransferMoney(fromAccount.AccountNumber, toAccount.AccountNumber, amount); //Assert Assert.That(result.IsSuccess, Is.True, "A 'success' result should be returned."); Assert.That(fromAccount.Balance, Is.EqualTo(originalFromBalance - amount), "The balance of the 'from' account is not correct after the transaction."); Assert.That(toAccount.Balance, Is.EqualTo(originalToBalance + amount), "The balance of the 'to' account is not correct after the transaction."); _accountRepositoryMock.Verify(repo => repo.GetByAccountNumber(fromAccount.AccountNumber), Times.Once, "The 'GetByAccountNumber' method of the repository should have been called once for the 'from' account number."); _accountRepositoryMock.Verify(repo => repo.GetByAccountNumber(toAccount.AccountNumber), Times.Once, "The 'GetByAccountNumber' method of the repository should have been called once for the 'to' account number."); _accountRepositoryMock.Verify(repo => repo.CommitChanges(), Times.Once, "The 'CommitChanges' method of the repository should have been called."); }
public void TransferMoney_InsufficientFundsAndNoYouthAccount_ShouldAllowTheBalanceToBeNegative() { //Arrange decimal originalFromBalance = Random.Next(10, 101); decimal originalToBalance = Random.Next(0, 1001); AccountType type = Random.NextAccountType(); while (type == AccountType.YouthAccount) { type = Random.NextAccountType(); } Account fromAccount = new AccountBuilder().WithBalance(originalFromBalance).WithType(type).Build(); Account toAccount = new AccountBuilder().WithBalance(originalToBalance).Build(); _accountRepositoryMock.Setup(repo => repo.GetByAccountNumber(fromAccount.AccountNumber)).Returns(fromAccount); _accountRepositoryMock.Setup(repo => repo.GetByAccountNumber(toAccount.AccountNumber)).Returns(toAccount); decimal amount = Random.Next(200, 1001); //Act Result result = _service.TransferMoney(fromAccount.AccountNumber, toAccount.AccountNumber, amount); //Assert Assert.That(result.IsSuccess, Is.True, "A 'success' result should be returned."); Assert.That(fromAccount.Balance, Is.EqualTo(originalFromBalance - amount), "The balance of the 'from' account is not correct after the transaction."); _accountRepositoryMock.Verify(repo => repo.CommitChanges(), Times.Once, "The 'CommitChanges' method of the repository should have been called."); }
public void _9_TransferButton_Click_ShouldShowTheTransferWindow() { //Arrange var fromAccount = new AccountBuilder().WithId().Build(); var allAccountsOfCustomer = new List <Account> { fromAccount, new AccountBuilder().WithId().Build() }; _accountRepositoryMock.Setup(repo => repo.GetAllAccountsOfCustomer(It.IsAny <int>())) .Returns(allAccountsOfCustomer); AddAccountsToTheGridAndSelectTheFirst(allAccountsOfCustomer); //Act _transferButton.FireClickEvent(); //Assert _windowDialogServiceMock.Verify( service => service.ShowTransferDialog(fromAccount, It.Is <IList <Account> >(accounts => accounts.All(account => allAccountsOfCustomer.Any(otherAccount => account.AccountNumber == otherAccount.AccountNumber)))), Times.Once, "A call to the 'ShowTranferDialog' method of the 'IWindowDialogService' should have been made correctly. " + "The first parameter should be the selected account in the datagrid. " + "The second parameter should be a list of all the accounts of the customer. " + "(Maybe the same list you retrieve in the constructor?)"); }
public void Add_ShouldAddANewAccountToTheDatabase() { //Arrange Customer existingCustomer; using (var context = CreateDbContext()) { existingCustomer = CreateExistingCustomer(context); } var newAccount = new AccountBuilder().WithCustomerId(existingCustomer.Id).Build(); using (var context = CreateDbContext()) { var repo = new AccountRepository(context); //Act repo.Add(newAccount); //Assert var addedAccount = context.Set <Account>().FirstOrDefault(a => a.CustomerId == existingCustomer.Id); Assert.That(addedAccount, Is.Not.Null, "The account is not added correctly to the database."); Assert.That(addedAccount.AccountNumber, Is.EqualTo(newAccount.AccountNumber), "The 'AccountNumber' is not saved correctly."); Assert.That(addedAccount.AccountType, Is.EqualTo(newAccount.AccountType), "The 'AccountType' is not saved correctly."); Assert.That(addedAccount.Balance, Is.EqualTo(newAccount.Balance), "The 'Balance' is not saved correctly."); } }
public void GetByAccountNumber_AccountExists_ShouldReturnMatchingAccount() { string accountNumber; using (var context = CreateDbContext()) { var existingCustomer = CreateExistingCustomer(context); Account existingAccount = new AccountBuilder().WithCustomerId(existingCustomer.Id) .WithBalance(RandomGenerator.Next(500, 1001)).Build(); context.Set <Account>().Add(existingAccount); context.SaveChanges(); accountNumber = existingAccount.AccountNumber; } using (var context = CreateDbContext()) { var repo = new AccountRepository(context); //Act Account retrievedAccount = repo.GetByAccountNumber(accountNumber); Assert.That(retrievedAccount, Is.Not.Null); Assert.That(retrievedAccount.AccountNumber, Is.EqualTo(accountNumber)); } }
public void Update_ShouldThrowAnInvalidOperationExceptionWhenTheBalanceIsUpdated() { //Arrange Account existingAccount; using (var context = CreateDbContext()) { var existingCustomer = CreateExistingCustomer(context); existingAccount = new AccountBuilder().WithCustomerId(existingCustomer.Id).Build(); context.Set <Account>().Add(existingAccount); context.SaveChanges(); } var existingAccountId = existingAccount.Id; using (var context = CreateDbContext()) { existingAccount = context.Set <Account>().Find(existingAccountId); existingAccount.Balance += 1; var repo = new AccountRepository(context); //Act + Assert Assert.That(() => repo.Update(existingAccount), Throws.InvalidOperationException, "No InvalidOperationException thrown. " + "Tip: an instance of the Entry class has a Property method that can be used to get the original and current value of a property of an entity. " + "So you can compare the original value and current value of the Balance property."); } }
public void _08_SaveAccountButton_Click_ShouldShowAnErrorWhenTheSelectedAccountIsInvalid() { //Arrange var existingAccount = new AccountBuilder().WithId().Build(); AddAccountsToTheGridAndSelectTheFirst(new List <Account> { existingAccount }); _errorTextBlock.Text = ""; var expectedErrorMessage = Guid.NewGuid().ToString(); _accountValidatorMock.Setup(validator => validator.IsValid(It.IsAny <Account>())) .Returns(ValidatorResult.Fail(expectedErrorMessage)); //Act _saveAccountButton.FireClickEvent(); //Assert _accountValidatorMock.Verify(validator => validator.IsValid(existingAccount), Times.Once, "The validator is not used correctly to check if the account is valid."); _accountRepositoryMock.Verify(repo => repo.Update(It.IsAny <Account>()), Times.Never, "The 'Update' method of the repository should not have been called."); _accountRepositoryMock.Verify(repo => repo.Add(It.IsAny <Account>()), Times.Never, "The 'Add' method of the repository should not have been called."); Assert.That(_errorTextBlock.Text, Is.EqualTo(expectedErrorMessage), "The ErrorTextBlock should contain the error message in de failed ValidatorResult."); }
public void Add_ShouldAddANewAccountToTheDatabaseForTheCorrectCustomer() { //Arrange var allOriginalCustomers = GetAllCustomers(); int existingCustomerId = allOriginalCustomers.First(c => c.CustomerId > 0).CustomerId; Account newAccount = new AccountBuilder().WithCustomerId(existingCustomerId).Build(); var allOriginalAccountsOfCustomer = GetAllAccounts().Where(account => account.CustomerId == existingCustomerId).ToList(); //Act _repository.Add(newAccount); //Assert var allAccounts = GetAllAccounts(); var allAccountsOfCustomer = allAccounts.Where(account => account.CustomerId == existingCustomerId).ToList(); Assert.That(allAccountsOfCustomer.Count, Is.EqualTo(allOriginalAccountsOfCustomer.Count + 1), () => "The number of accounts in the database should be increased by one."); var addedAccount = allAccounts.FirstOrDefault(account => account.AccountNumber == newAccount.AccountNumber); Assert.That(addedAccount, Is.Not.Null, () => "No account with the added account number can be found in the database afterwards."); Assert.That(addedAccount.Id, Is.GreaterThan(0), () => "The account was added with 'IDENTITY_INSERT' on. " + "You should let the database generate a value for the 'Id' column. " + "Until you fix this problem, other tests might also behave strangely."); Assert.That(addedAccount.CustomerId, Is.EqualTo(existingCustomerId), () => "The customerId of the added account is not correct."); Assert.That(addedAccount.AccountType, Is.EqualTo(newAccount.AccountType), () => "The 'AccountType' is not saved correctly."); Assert.That(addedAccount.Balance, Is.EqualTo(newAccount.Balance), () => "The 'Balance' is not saved correctly."); }
public void Add_ShouldCreateAndCloseConnection() { var allOriginalCustomers = GetAllCustomers(); int existingCustomerId = allOriginalCustomers.First(c => c.CustomerId > 0).CustomerId; Account newAccount = new AccountBuilder().WithCustomerId(existingCustomerId).Build(); AssertConnectionIsCreatedAndClosed(() => _repository.Add(newAccount)); }
public void Add_ShouldThrowArgumentExceptionWhenCustomerIdIsNotSet() { //Arrange Account newAccount = new AccountBuilder().WithCustomerId(0).Build(); //Act + Assert Assert.That(() => _repository.Add(newAccount), Throws.ArgumentException, () => "No ArgumentException is thrown when 'CustomerId' is zero."); }
public void Add_ShouldThrowArgumentExceptionWhenTheAccountIdIsNotZero() { //Arrange Account newAccount = new AccountBuilder().WithCustomerId().WithId().Build(); //Act + Assert Assert.That(() => _repository.Add(newAccount), Throws.ArgumentException, () => "No ArgumentException is thrown when Id is greather than zero"); }
public void Update_ShouldThrowArgumentExceptionWhenCustomerIdIsNotSet() { //Arrange Account existingAccountWithoutCustomer = new AccountBuilder().WithId().WithCustomerId(0).Build(); //Act + Assert Assert.That(() => _repository.Update(existingAccountWithoutCustomer), Throws.ArgumentException, () => "No ArgumentException is thrown when the CustomerId is zero."); }
public void Update_ShouldThrowArgumentExceptionWhenTheAccountIdIsZero() { //Arrange Account newAccount = new AccountBuilder().WithCustomerId().WithId(0).Build(); //Act + Assert Assert.That(() => _repository.Update(newAccount), Throws.ArgumentException, () => "No ArgumentException is thrown when the Id is zero."); }
public void IsValid_ShouldPassForValidAccount() { //Arrange var account = new AccountBuilder().WithCustomerId(_existingCustomers.First().Id).Build(); //Act var result = _validator.IsValid(account); //Assert Assert.That(result.IsValid, Is.True); }
public void Update_ShouldThrowArgumentExceptionWhenTheAccountDoesNotExists() { //Arrange var newAccount = new AccountBuilder().WithId(0).Build(); using (var context = CreateDbContext()) { var repo = new AccountRepository(context); //Act + Assert Assert.That(() => repo.Update(newAccount), Throws.ArgumentException); } }
public void Add_ShouldBeAbleToHandleNullForAccountNumber() { //Arrange var allOriginalCustomers = GetAllCustomers(); int existingCustomerId = allOriginalCustomers.First().CustomerId; Account newAccount = new AccountBuilder() .WithCustomerId(existingCustomerId) .WithAccountNumber(null) .Build(); //Act + Assert AssertDoesNotThrowSqlParameterException(() => _repository.Add(newAccount)); }
public void ShouldImplementINotifyPropertyChangedAndUseItForBalance() { var account = new AccountBuilder().Build(); INotifyPropertyChanged notifier = account as INotifyPropertyChanged; Assert.That(notifier, Is.Not.Null, () => "INotifyPropertyChanged is not implemented."); var notifyForBalancePropertyReceived = false; notifier.PropertyChanged += (sender, e) => { notifyForBalancePropertyReceived = e.PropertyName == "Balance"; }; account.Balance += 1; Assert.That(notifyForBalancePropertyReceived, Is.True, () => "No 'PropertyChanged' event it triggerd when the 'Balance' property changes."); }
public void Add_ShouldSetTheIdOnTheInsertedAccountInstance() { //Arrange var allOriginalCustomers = GetAllCustomers(); int existingCustomerId = allOriginalCustomers.First().CustomerId; Account newAccount = new AccountBuilder().WithId(0).WithCustomerId(existingCustomerId).Build(); //Act _repository.Add(newAccount); //Assert Assert.That(newAccount.Id, Is.GreaterThan(0), () => "After calling 'Add', the 'Id' property of the 'newAccount' object passed as parameter should be greater than zero."); }
public void Add_ShouldNotSaveCustomerRelationInADisconnectedScenario() { //Arrange Customer existingCustomer; using (var context = CreateDbContext()) { existingCustomer = CreateExistingCustomer(context); } var newAccount = new AccountBuilder().WithCustomerId(existingCustomer.Id).Build(); existingCustomer.TrySetCity(null); existingCustomer.TrySetAccounts(null); newAccount.TrySetCustomer(existingCustomer); using (var context = CreateDbContext()) { var repo = new AccountRepository(context); //Act try { repo.Add(newAccount); } catch (DbUpdateException updateException) { if (updateException.InnerException != null && updateException.InnerException.Message.ToLower() .Contains("unique constraint")) { Assert.Fail( "If the 'Customer' navigation property is set to an untracked instance of customer, " + "the application tries to add the customer to the database. " + "Make sure relations are not saved."); } else { throw; } } catch (Exception) { Assert.Fail("Something went wrong when adding the Account."); } } }
public void _5_SaveAccountButton_Click_ShouldUpdateASelectedExistingAccountInTheDatabase() { //Arrange var existingAccount = new AccountBuilder().WithId().Build(); AddAccountsToTheGridAndSelectTheFirst(new List <Account> { existingAccount }); //Act _saveAccountButton.FireClickEvent(); //Assert _accountRepositoryMock.Verify(repo => repo.Update(existingAccount), Times.Once, "The 'Update' method of the repository is not called correctly."); _accountRepositoryMock.Verify(repo => repo.Add(It.IsAny <Account>()), Times.Never, "The 'Add' method of the repository should not have been called."); }
public void _6_SaveAccountButton_Click_ShouldAddASelectedNewAccountToTheDatabase() { //Arrange var newAccount = new AccountBuilder().WithId(0).Build(); AddNewAccountToTheGridAndSelectIt(newAccount); //Act _saveAccountButton.FireClickEvent(); //Assert _accountRepositoryMock.Verify(repo => repo.Add(newAccount), Times.Once, "The 'Add' method of the repository is not called correctly."); _accountRepositoryMock.Verify(repo => repo.Update(It.IsAny <Account>()), Times.Never, "The 'Update' method of the repository should not have been called."); Assert.That(_datagrid.CanUserAddRows, Is.False); }
public void Update_ShouldUpdateAnExistingAccountInTheDatabase() { //Arrange Account existingAccount; IList <Account> allOriginalAccounts; using (var context = CreateDbContext()) { var existingCustomer = CreateExistingCustomer(context); existingAccount = new AccountBuilder().WithCustomerId(existingCustomer.Id).Build(); context.Set <Account>().Add(existingAccount); context.SaveChanges(); allOriginalAccounts = context.Set <Account>().ToList(); } var existingAccountId = existingAccount.Id; var newAccountNumber = Guid.NewGuid().ToString(); var newAccountType = AccountType.PremiumAccount; using (var context = CreateDbContext()) { existingAccount = context.Set <Account>().Find(existingAccountId); existingAccount.AccountNumber = newAccountNumber; existingAccount.AccountType = newAccountType; var repo = new AccountRepository(context); //Act repo.Update(existingAccount); } using (var context = CreateDbContext()) { var updatedAccount = context.Set <Account>().Find(existingAccountId); //Assert var allAccounts = context.Set <Account>().ToList(); Assert.That(allAccounts, Has.Count.EqualTo(allOriginalAccounts.Count), "The amount of accounts in the database changed."); Assert.That(updatedAccount.AccountNumber, Is.EqualTo(newAccountNumber), "Accountnumber is not updated properly."); Assert.That(updatedAccount.AccountType, Is.EqualTo(newAccountType), "Account type is not updated properly."); } }
public void IsValid_ShouldFailOnNonExistingCustomer() { //Arrange var nonExistingCustomerId = _random.Next(); while (_existingCustomers.Any(c => c.Id == nonExistingCustomerId)) { nonExistingCustomerId = _random.Next(); } var account = new AccountBuilder().WithCustomerId(nonExistingCustomerId).Build(); //Act var result = _validator.IsValid(account); //Assert Assert.That(result.IsValid, Is.False, "Result should be invalid."); Assert.That(result.Message, Is.Not.Null.And.Not.Empty, "Message should not be empty."); }
public void Add_ShouldNotBeVunerableToSQLInjectionAttacks() { //Arrange var allOriginalCustomers = GetAllCustomers(); int existingCustomerId = allOriginalCustomers.First(c => c.CustomerId > 0).CustomerId; Account newAccount = new AccountBuilder().WithCustomerId(existingCustomerId).Build(); var sqlInjectionText = $"',{existingCustomerId},{(int)AccountType.PaymentAccount},{existingCustomerId}); DELETE FROM dbo.Accounts; --"; newAccount.AccountNumber = sqlInjectionText; //Act _repository.Add(newAccount); //Assert var allAccounts = GetAllAccounts(); Assert.That(allAccounts.Count, Is.GreaterThan(0), () => "A SQL Injection attack that deletes all 'Accounts' from the database succeeded. " + "This may also affect the outcome of other tests."); }
public void AddNewAccountForCustomer_AccountNumberAlreadyInUse_ShouldFail() { //Arrange Customer customer = new CustomerBuilder().WithId().Build(); AccountType type = Random.NextAccountType(); string accountNumber = Guid.NewGuid().ToString(); var existingAccount = new AccountBuilder().Build(); _accountRepositoryMock.Setup(repo => repo.GetByAccountNumber(accountNumber)).Returns(existingAccount); //Act Result result = _service.AddNewAccountForCustomer(customer, accountNumber, type); //Assert Assert.That(result.IsSuccess, Is.False); Assert.That(result.Message, Contains.Substring("exist").IgnoreCase, "The result message should contain the word 'exist'."); _accountRepositoryMock.Verify(repo => repo.Add(It.IsAny <Account>()), Times.Never, "The 'Add' method of the repository should not have been called."); }
public void Add_ShouldThrowArgumentExceptionWhenTheAccountAlreadyExists() { //Arrange Account existingAccount; using (var context = CreateDbContext()) { var existingCustomer = CreateExistingCustomer(context); existingAccount = new AccountBuilder().WithCustomerId(existingCustomer.Id).Build(); context.Set <Account>().Add(existingAccount); context.SaveChanges(); } using (var context = CreateDbContext()) { var repo = new AccountRepository(context); //Act + Assert Assert.That(() => repo.Add(existingAccount), Throws.ArgumentException); } }
public void TransferMoney_ShouldGetThe2AccountsChangeTheBalancesAndSaveTheChanges() { Account fromAccount; Account toAccount; using (var context = CreateDbContext()) { var existingCustomer = CreateExistingCustomer(context); fromAccount = new AccountBuilder().WithCustomerId(existingCustomer.Id) .WithBalance(RandomGenerator.Next(500, 1001)).Build(); toAccount = new AccountBuilder().WithCustomerId(existingCustomer.Id).Build(); context.Set <Account>().Add(fromAccount); context.Set <Account>().Add(toAccount); context.SaveChanges(); } var fromAccountId = fromAccount.Id; var toAccountId = toAccount.Id; decimal transferAmount = RandomGenerator.Next(100, 401); var expectedFromBalance = fromAccount.Balance - transferAmount; var expectedToBalance = toAccount.Balance + transferAmount; using (var context = CreateDbContext()) { var repo = new AccountRepository(context); //Act repo.TransferMoney(fromAccountId, toAccountId, transferAmount); } using (var context = CreateDbContext()) { var updatedFromAccount = context.Set <Account>().Find(fromAccountId); var updatedToAccount = context.Set <Account>().Find(toAccountId); //Assert Assert.That(updatedFromAccount.Balance, Is.EqualTo(expectedFromBalance), "Balance of the 'from account' is not updated correctly."); Assert.That(updatedToAccount.Balance, Is.EqualTo(expectedToBalance), "Balance of the 'to account' is not updated correctly."); } }
public void CommitChanges_ShouldSaveChangesOnTrackedEntities() { string accountNumber; using (var context = CreateDbContext()) { var existingCustomer = CreateExistingCustomer(context); Account existingAccount = new AccountBuilder().WithCustomerId(existingCustomer.Id) .WithBalance(RandomGenerator.Next(500, 1001)).Build(); context.Set <Account>().Add(existingAccount); context.SaveChanges(); accountNumber = existingAccount.AccountNumber; } decimal newBalance; using (var context = CreateDbContext()) { var repo = new AccountRepository(context); Account account = context.Set <Account>().Find(accountNumber); Assert.That(account, Is.Not.Null, "Cannot retrieve saved account from database."); newBalance = account.Balance - RandomGenerator.Next(10, 101); account.Balance = newBalance; //Act repo.CommitChanges(); } using (var context = CreateDbContext()) { var updatedAccount = context.Set <Account>().Find(accountNumber); Assert.That(updatedAccount, Is.Not.Null, "Cannot retrieve saved account from database."); //Assert Assert.That(updatedAccount.Balance, Is.EqualTo(newBalance), "Balance of the 'account' is not updated correctly."); } }
public void TransferMoney_InsufficientFundsInYouthAccount_ShouldReturnFailure() { //Arrange decimal originalFromBalance = Random.Next(10, 101); decimal originalToBalance = Random.Next(0, 1001); Account fromAccount = new AccountBuilder().WithBalance(originalFromBalance).WithType(AccountType.YouthAccount).Build(); Account toAccount = new AccountBuilder().WithBalance(originalToBalance).Build(); _accountRepositoryMock.Setup(repo => repo.GetByAccountNumber(fromAccount.AccountNumber)).Returns(fromAccount); _accountRepositoryMock.Setup(repo => repo.GetByAccountNumber(toAccount.AccountNumber)).Returns(toAccount); decimal amount = Random.Next(200, 1001); //Act Result result = _service.TransferMoney(fromAccount.AccountNumber, toAccount.AccountNumber, amount); //Assert Assert.That(result.IsSuccess, Is.False, "A 'failure' result should be returned."); _accountRepositoryMock.Verify(repo => repo.CommitChanges(), Times.Never, "The 'CommitChanges' method of the repository should not have been called."); }
public void _05_SaveAccountButton_Click_ShouldUpdateASelectedExistingAccountInTheDatabase() { //Arrange var existingAccount = new AccountBuilder().WithId().Build(); AddAccountsToTheGridAndSelectTheFirst(new List <Account> { existingAccount }); _errorTextBlock.Text = "Some error message"; //Act _saveAccountButton.FireClickEvent(); //Assert _accountRepositoryMock.Verify(repo => repo.Update(existingAccount), Times.Once, "The 'Update' method of the repository is not called correctly."); _accountRepositoryMock.Verify(repo => repo.Add(It.IsAny <Account>()), Times.Never, "The 'Add' method of the repository should not have been called."); _accountValidatorMock.Verify(validator => validator.IsValid(existingAccount), Times.Once, "The validator is not used correctly to check if the customer is valid."); Assert.That(_errorTextBlock.Text, Is.Empty, "When saving, previous error messages should be cleared from the ErrorTextBlock."); }