コード例 #1
0
        public void _1_ShouldHaveConfiguredTheAccountDataGridColumnsCorrectlyInXaml()
        {
            //Arrange
            var customer = new CustomerBuilder().WithId().Build();

            InitializeWindow(customer, _accountRepositoryMock.Object, _windowDialogServiceMock.Object);

            var dataGridTextColumns = _datagrid.Columns.OfType <DataGridTextColumn>().ToList();
            var textColumnBindings  = dataGridTextColumns.Select(column => column.Binding).OfType <Binding>().ToList();

            Assert.That(textColumnBindings, Has.One.Matches((Binding binding) => binding.Path.Path == "AccountNumber"),
                        () => "Could not find a binding for the 'AccountNumber' property.");
            Assert.That(textColumnBindings, Has.One.Matches((Binding binding) => binding.Path.Path == "Balance"),
                        () => "Could not find a binding for the 'Balance' property.");

            var balanceColumn = dataGridTextColumns.First(column => (column.Header as string)?.ToLower() == "balance");

            Assert.That(balanceColumn.IsReadOnly, Is.True,
                        () =>
                        "The 'Balance' column should be readonly ('IsReadOnly') " +
                        "so that it is not possible to edit balances directly in the datagrid.");
            var balanceBinding = (Binding)balanceColumn.Binding;

            Assert.That(balanceBinding.Mode, Is.EqualTo(BindingMode.OneWay),
                        () =>
                        "Set the binding mode of the balance column so that " +
                        "updates in the UI are ignored, but updates on the source object are detected.");
        }
コード例 #2
0
        public void Add_ShouldCreateAndCloseConnection()
        {
            var      existingCity = GetAllCities().First();
            Customer newCustomer  = new CustomerBuilder().WithId(0).WithZipCode(existingCity.ZipCode).Build();

            AssertConnectionIsCreatedAndClosed(() => _repository.Add(newCustomer));
        }
コード例 #3
0
        public void Add_ShouldAddANewAccountToTheDatabase()
        {
            //Arrange
            Customer existingCustomer;

            using (var context = CreateDbContext())
            {
                City existingCity = CreateExistingCity(context);
                existingCustomer = new CustomerBuilder().WithZipCode(existingCity.ZipCode).Build();
                context.Set <Customer>().Add(existingCustomer);
                context.SaveChanges();
            }

            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.");
            }
        }
コード例 #4
0
        public void _08_SaveCustomerButton_Click_ShouldShowAnErrorWhenTheSelectedCustomerIsInvalid()
        {
            //Arrange
            var existingCustomer = new CustomerBuilder().WithId().Build();

            AddCustomerToTheGridAndSelectIt(existingCustomer);
            _errorTextBlock.Text = "";

            var expectedErrorMessage = Guid.NewGuid().ToString();

            _customerValidatorMock.Setup(validator => validator.IsValid(It.IsAny <Customer>()))
            .Returns(ValidatorResult.Fail(expectedErrorMessage));

            //Act
            _saveCustomerButton.FireClickEvent();

            //Assert
            _customerValidatorMock.Verify(validator => validator.IsValid(existingCustomer), Times.Once,
                                          "The validator is not used correctly to check if the customer is valid.");
            _customerRepositoryMock.Verify(repo => repo.Update(It.IsAny <Customer>()), Times.Never,
                                           "The 'Update' method of the repository should not have been called.");
            _customerRepositoryMock.Verify(repo => repo.Add(It.IsAny <Customer>()), 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.");
        }
コード例 #5
0
        public void Add_ShouldAddANewCustomerToTheDatabase()
        {
            //Arrange
            var      allOriginalCustomers = GetAllCustomers();
            var      existingCity         = GetAllCities().First();
            Customer newCustomer          = new CustomerBuilder().WithId(0).WithZipCode(existingCity.ZipCode).Build();

            //Act
            _repository.Add(newCustomer);

            //Assert
            var allCustomersAfterInsert = GetAllCustomers();

            Assert.That(allCustomersAfterInsert.Count, Is.EqualTo(allOriginalCustomers.Count + 1),
                        () => "The number of customers in the database should be increased by one.");

            var addedCustomer = allCustomersAfterInsert.FirstOrDefault(customer => customer.Name == newCustomer.Name);

            Assert.That(addedCustomer, Is.Not.Null,
                        () => "No customer with the added name can be found in the database afterwards.");
            Assert.That(addedCustomer.CustomerId, Is.GreaterThan(0),
                        () => "The customer was added with 'IDENTITY_INSERT' on. " +
                        "You should let the database generate a value for the 'CustomerId' column. " +
                        "Until you fix this problem, other tests might also behave strangely.");
            Assert.That(addedCustomer.Address, Is.EqualTo(newCustomer.Address),
                        () => "The 'Address' is not saved correctly.");
            Assert.That(addedCustomer.CellPhone, Is.EqualTo(newCustomer.CellPhone),
                        () => "The 'CellPhone' is not saved correctly.");
            Assert.That(addedCustomer.FirstName, Is.EqualTo(newCustomer.FirstName),
                        () => "The 'FirstName' is not saved correctly.");
            Assert.That(addedCustomer.Name, Is.EqualTo(newCustomer.Name),
                        () => "The 'Name' is not saved correctly.");
            Assert.That(addedCustomer.ZipCode, Is.EqualTo(newCustomer.ZipCode),
                        () => "The 'ZipCode' is not saved correctly.");
        }
コード例 #6
0
        private void AddAccountsToTheGridAndSelectTheFirst(IList <Account> allAccountsOfCustomer)
        {
            var customer = new CustomerBuilder().WithId().WithAccounts(allAccountsOfCustomer).Build();

            InitializeWindow(customer);

            _datagrid.SelectedIndex = 0; //select the account
        }
コード例 #7
0
        public void Add_ShouldThrowArgumentExceptionWhenZipCodeIsNotSet()
        {
            //Arrange
            Customer newCustomer = new CustomerBuilder().WithId(0).WithZipCode(0).Build();

            //Act + Assert
            Assert.That(() => _repository.Add(newCustomer), Throws.ArgumentException,
                        () => "No ArgumentException is thrown when 'ZipCode' is zero.");
        }
コード例 #8
0
        public void Add_ShouldThrowArgumentExceptionWhenTheCustomerIdIsNotZero()
        {
            //Arrange
            Customer newCustomer = new CustomerBuilder().WithId().Build();

            //Act + Assert
            Assert.That(() => _repository.Add(newCustomer), Throws.ArgumentException,
                        () => "No ArgumentException is thrown when CustomerId is greather than zero");
        }
コード例 #9
0
        public void Update_ShouldThrowArgumentExceptionWhenZipCodeIsNotSet()
        {
            //Arrange
            Customer existingCustomerWithoutZipCode = new CustomerBuilder().WithId().WithZipCode(0).Build();

            //Act + Assert
            Assert.That(() => _repository.Update(existingCustomerWithoutZipCode), Throws.ArgumentException,
                        () => "No ArgumentException is thrown when the 'ZipCode' is zero.");
        }
コード例 #10
0
        internal Customer CreateExistingCustomer(BankContext context)
        {
            City     existingCity     = CreateExistingCity(context);
            Customer existingCustomer = new CustomerBuilder().WithZipCode(existingCity.ZipCode).Build();

            context.Set <Customer>().Add(existingCustomer);
            context.SaveChanges();
            return(existingCustomer);
        }
コード例 #11
0
        public void Update_ShouldThrowArgumentExceptionWhenCustomerIdIsNotSet()
        {
            //Arrange
            Customer newCustomer = new CustomerBuilder().WithId(0).Build();

            //Act + Assert
            Assert.That(() => _repository.Update(newCustomer), Throws.ArgumentException,
                        () => "No ArgumentException is thrown when the 'CustomerId' is zero.");
        }
コード例 #12
0
        private void AddAccountsToTheGridAndSelectTheFirst(IList <Account> allAccountsOfCustomer)
        {
            var customer = new CustomerBuilder().WithId().Build();

            _accountRepositoryMock.Setup(repo => repo.GetAllAccountsOfCustomer(It.IsAny <int>()))
            .Returns(allAccountsOfCustomer);

            InitializeWindow(customer, _accountRepositoryMock.Object, _windowDialogServiceMock.Object);

            _datagrid.SelectedIndex = 0; //select the account
        }
コード例 #13
0
        public void Validate_ZipCodeIsNotValid_ShouldReturnFalse()
        {
            List <City> validCities    = GenerateSomeValidCities();
            int         maxZipCode     = validCities.Max(c => c.ZipCode);
            int         invalidZipCode = maxZipCode + 1;

            Customer customer = new CustomerBuilder().WithZipCode(invalidZipCode).Build();

            Assert.That(customer.Validate(validCities).IsSuccess, Is.False,
                        "Should return failure when none of the valid cities has the zipcode of the customer.");
        }
コード例 #14
0
        public void IsValid_ShouldPassForValidCustomer()
        {
            //Arrange
            var customer = new CustomerBuilder().WithZipCode(_existingCities.First().ZipCode).Build();

            //Act
            var result = _validator.IsValid(customer);

            //Assert
            Assert.That(result.IsValid, Is.True);
        }
コード例 #15
0
        public void IsValid_ShouldFailOnNonExistingZipcode()
        {
            //Arrange
            var customer = new CustomerBuilder().WithZipCode(_nonExistingZipCode).Build();

            //Act
            var result = _validator.IsValid(customer);

            //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.");
        }
コード例 #16
0
        public void Update_ShouldUpdateAnExistingCustomerInTheDatabase()
        {
            //Arrange
            IList <Customer> allOriginalCustomers;
            City             existingCity;
            Customer         existingCustomer;

            using (var context = CreateDbContext())
            {
                existingCity     = CreateExistingCity(context);
                existingCustomer = new CustomerBuilder().WithZipCode(existingCity.ZipCode).Build();
                context.Set <Customer>().Add(existingCustomer);
                context.SaveChanges();
                allOriginalCustomers = context.Set <Customer>().ToList();
            }

            var existingCustomerId = existingCustomer.Id;
            var newAddress         = Guid.NewGuid().ToString();
            var newCellPhone       = Guid.NewGuid().ToString();
            var newFirstName       = Guid.NewGuid().ToString();
            var newLastName        = Guid.NewGuid().ToString();

            using (var context = CreateDbContext())
            {
                existingCustomer           = context.Set <Customer>().Find(existingCustomerId);
                existingCustomer.Address   = newAddress;
                existingCustomer.CellPhone = newCellPhone;
                existingCustomer.FirstName = newFirstName;
                existingCustomer.Name      = newLastName;
                existingCustomer.ZipCode   = existingCity.ZipCode;
                var repo = new CustomerRepository(context);

                //Act
                repo.Update(existingCustomer);
            }

            using (var context = CreateDbContext())
            {
                var updatedCustomer = context.Set <Customer>().Find(existingCustomerId);

                //Assert
                var allCustomers = context.Set <Customer>().ToList();
                Assert.That(allCustomers, Has.Count.EqualTo(allOriginalCustomers.Count),
                            "The amount of customers in the database changed.");

                Assert.That(updatedCustomer.Address, Is.EqualTo(newAddress), "Address is not updated properly.");
                Assert.That(updatedCustomer.CellPhone, Is.EqualTo(newCellPhone), "CellPhone is not updated properly.");
                Assert.That(updatedCustomer.FirstName, Is.EqualTo(newFirstName), "FirstName is not updated properly.");
                Assert.That(updatedCustomer.Name, Is.EqualTo(newLastName), "Name is not updated properly.");
                Assert.That(updatedCustomer.ZipCode, Is.EqualTo(existingCity.ZipCode), "ZipCode is not updated properly.");
            }
        }
コード例 #17
0
        public void Update_ShouldThrowArgumentExceptionWhenTheCustomerDoesNotExists()
        {
            //Arrange
            var newCustomer = new CustomerBuilder().WithId(0).Build();

            using (var context = CreateDbContext())
            {
                var repo = new CustomerRepository(context);

                //Act + Assert
                Assert.That(() => repo.Update(newCustomer), Throws.ArgumentException);
            }
        }
コード例 #18
0
        public void Add_ShouldBeAbleToHandlePropertiesThatAreNull()
        {
            //Arrange
            var existingCity = GetAllCities().First();

            Customer newCustomer = new CustomerBuilder()
                                   .WithId(0)
                                   .WithZipCode(existingCity.ZipCode)
                                   .WithNullProperties().Build();

            //Act
            AssertDoesNotThrowSqlParameterException(() => _repository.Add(newCustomer));
        }
コード例 #19
0
        public void Validate_ValidCustomer_ShouldReturnTrue()
        {
            //Arrange
            List <City> validCities = GenerateSomeValidCities();
            City        city        = validCities[Random.Next(0, validCities.Count)];
            Customer    customer    = new CustomerBuilder().WithZipCode(city.ZipCode).Build();


            //Act
            Result result = customer.Validate(validCities);

            //Assert
            Assert.That(result.IsSuccess, Is.True);
        }
コード例 #20
0
        public void _3_ShouldShowTheFullNameOfTheCustomerInItsTitle()
        {
            //Arrange
            var customer = new CustomerBuilder().WithId().Build();

            //Act
            InitializeWindow(customer, _accountRepositoryMock.Object, _windowDialogServiceMock.Object);

            //Assert
            var fullName = $"{customer.FirstName} {customer.Name}".Trim();

            Assert.That(_window.Title, Contains.Substring(fullName),
                        () => "Title does not contain the correct concatenation of 'FirstName', whitespace, 'LastName'.");
        }
コード例 #21
0
        public void Add_ShouldSetTheCustomerIdOnTheInsertedCustomerInstance()
        {
            //Arrange
            var      existingCity = GetAllCities().First();
            Customer newCustomer  = new CustomerBuilder().WithId(0).WithZipCode(existingCity.ZipCode).Build();

            //Act
            _repository.Add(newCustomer);

            //Assert
            Assert.That(newCustomer.CustomerId, Is.GreaterThan(0),
                        () =>
                        "After calling 'Add', the 'CustomerId' property of the 'newCustomer' object passed as parameter should be greater than zero.");
        }
コード例 #22
0
        public void _02_Constructor_ShouldUseTheAccountsOfTheCustomerAsTheSourceForTheListView()
        {
            //Arrange
            Customer customer = new CustomerBuilder().WithId().WithAccounts().Build();

            //Act
            InitializeWindow(customer);

            //Assert
            ICollection <Account> accounts = customer.TryGetAccounts();

            Assert.That(_listView.ItemsSource, Is.SameAs(accounts),
                        "The ItemsSource of the ListView should be the very same list of Accounts of the customer.");
        }
コード例 #23
0
        public void _02_ShouldShowTheFullNameOfTheCustomerInItsTitle()
        {
            //Arrange
            var customer = new CustomerBuilder().WithId().WithAccounts(new List <Account>()).Build();

            //Act
            InitializeWindow(customer);

            //Assert
            var fullName = $"{customer.FirstName} {customer.Name}".Trim();

            Assert.That(_window.Title, Contains.Substring(fullName),
                        "Title does not contain the correct concatenation of 'FirstName', whitespace, 'LastName'.");
        }
コード例 #24
0
        private void AddNewAccountToTheGridAndSelectIt(Account newAccount)
        {
            var customer           = new CustomerBuilder().WithId().Build();
            var accountsItemSource = new List <Account>();

            _accountRepositoryMock.Setup(repo => repo.GetAllAccountsOfCustomer(It.IsAny <int>()))
            .Returns(accountsItemSource);

            InitializeWindow(customer, _accountRepositoryMock.Object, _windowDialogServiceMock.Object);

            _datagrid.CanUserAddRows = true;
            accountsItemSource.Insert(0, newAccount);

            _datagrid.SelectedIndex = 0; //select the account
        }
コード例 #25
0
        public void _04_AddAccountButton_Click_ShouldAddANewRow()
        {
            //Arrange
            var customer = new CustomerBuilder().WithId().WithAccounts(new List <Account>()).Build();

            InitializeWindow(customer);

            //Act
            _addAccountButton.FireClickEvent();

            //Assert
            Assert.That(_datagrid.CanUserAddRows, Is.True,
                        "The property 'CanUserAddRows' of the datagrid should be true. " +
                        "This ensures that a new (empty) row is added to the datagrid.");
        }
コード例 #26
0
        public void _10_ShowAccountsButton_Click_ShouldShowTheAccountsWindowForTheSelectedCustomer()
        {
            //Arrange
            var existingCustomer = new CustomerBuilder().WithId().Build();

            AddCustomerToTheGridAndSelectIt(existingCustomer);

            //Act
            _showAccountsButton.FireClickEvent();

            //Assert
            _windowDialogServiceMock.Verify(service => service.ShowAccountDialogForCustomer(existingCustomer), Times.Once,
                                            "A call to the 'ShowAccountDialogForCustomer' method of the 'IWindowDialogService' should have been made correctly. " +
                                            "The parameter should be the selected customer in the datagrid. ");
        }
コード例 #27
0
        public void _04_AddAccountButton_Click_AccountServiceSucceeds_ShouldResetUI()
        {
            //Arrange
            Customer customer = new CustomerBuilder().WithId().WithAccounts().Build();

            InitializeWindow(customer);

            _errorTextBlock.Text = "Some previous error message";

            string accountNumber = Guid.NewGuid().ToString();

            _accountNumberTextBox.Text = accountNumber;

            AccountType type = Random.NextAccountType();

            _typeComboBox.SelectedValue = type;

            ICollection <Account> accounts = customer.TryGetAccounts();

            Result successResult = Result.Success();

            _accountServiceMock
            .Setup(service =>
                   service.AddNewAccountForCustomer(It.IsAny <Customer>(), It.IsAny <string>(), It.IsAny <AccountType>()))
            .Returns(successResult).Callback(() =>
            {
                accounts.Add(new AccountBuilder().WithCustomerId(customer.Id).Build());
            });

            //Act
            _addAccountButton.FireClickEvent();

            //Assert
            Assert.That(string.IsNullOrEmpty(_errorTextBlock.Text), Is.True,
                        "The error TextBlock should be empty after the add button is clicked. Any previous error message should be cleared.");

            _accountServiceMock.Verify(service => service.AddNewAccountForCustomer(customer, accountNumber, type), Times.Once,
                                       "The AddNewAccountForCustomer method of the account service is not called correctly.");

            Assert.That(string.IsNullOrEmpty(_accountNumberTextBox.Text), Is.True,
                        "The account TextBox should be cleared after the add succeeds.");

            ItemCollection items = _listView.Items;

            Assert.That(items.Count, Is.EqualTo(accounts.Count),
                        "The 'Items' collection of the ListView should have one account more after the account is added. " +
                        "Tip: try to call the 'Refresh' method on the 'Items' collection to tell WPF that the number of accounts of the customer changed.");
        }
コード例 #28
0
        public void _4_AddAccountButton_Click_ShouldAddANewRow()
        {
            //Arrange
            var customer = new CustomerBuilder().WithId().Build();

            InitializeWindow(customer, _accountRepositoryMock.Object, _windowDialogServiceMock.Object);

            //Act
            _addAccountButton.FireClickEvent();

            //Assert
            Assert.That(_datagrid.CanUserAddRows, Is.True,
                        () =>
                        "The property 'CanUserAddRows' of the datagrid should be true. " +
                        "This ensures that a new (empty) row is added to the datagrid.");
        }
コード例 #29
0
        public void _5_SaveCustomerButton_Click_ShouldUpdateASelectedExistingCustomerInTheDatabase()
        {
            //Arrange
            var existingCustomer = new CustomerBuilder().WithId().Build();

            AddCustomerToTheGridAndSelectIt(existingCustomer);

            //Act
            _saveCustomerButton.FireClickEvent();

            //Assert
            _customerRepositoryMock.Verify(repo => repo.Update(existingCustomer), Times.Once,
                                           "The 'Update' method of the repository is not called correctly.");
            _customerRepositoryMock.Verify(repo => repo.Add(It.IsAny <Customer>()), Times.Never,
                                           "The 'Add' method of the repository should not have been called.");
        }
コード例 #30
0
        public void _11_TransferButton_ShouldDoNothingWhenNoAccountIsSelected()
        {
            //Arrange
            var customer = new CustomerBuilder().WithId().WithAccounts(new List <Account>()).Build();

            InitializeWindow(customer);
            _datagrid.SelectedIndex = -1;

            //Act
            Assert.That(() => _transferButton.FireClickEvent(), Throws.Nothing,
                        () => "An exception occurs when nothing is selected.");

            //Assert
            _windowDialogServiceMock.Verify(
                service => service.ShowTransferDialog(It.IsAny <Account>(), It.IsAny <IList <Account> >()), Times.Never,
                "The 'ShowTransferDialog' method of the 'IWindowDialogService' should not have been called.");
        }