public void NullArgumentChecks() { using (FakeEmployeeContext ctx = new FakeEmployeeContext()) { Utilities.CheckNullArgumentException(() => { var c = new FakeEmployeeContext(null, new Department[0]); c.Dispose(); }, "employees", "ctor"); Utilities.CheckNullArgumentException(() => { var c = new FakeEmployeeContext(new Employee[0], null); c.Dispose(); }, "departments", "ctor"); Utilities.CheckNullArgumentException(() => { ctx.IsObjectTracked(null); }, "entity", "IsObjectTracked"); } }
public void ExternalAddToDepartmentLookup() { using (FakeEmployeeContext ctx = BuildContextWithData()) { EmployeeViewModel vm = BuildViewModel(ctx); DepartmentViewModel currentDepartment = vm.Department; DepartmentViewModel newDepartment = new DepartmentViewModel(new Department()); vm.DepartmentLookup.Add(newDepartment); Assert.IsTrue(vm.DepartmentLookup.Contains(newDepartment), "New department should have been added to DepartmentLookup."); Assert.AreSame(currentDepartment, vm.Department, "Assigned Department should not have changed."); Assert.IsFalse(ctx.IsObjectTracked(newDepartment.Model), "ViewModel is not responsible for adding departments created externally."); } }
public void ExternalRemoveDepartmentLookup() { using (FakeEmployeeContext ctx = BuildContextWithData()) { EmployeeViewModel vm = BuildViewModel(ctx); DepartmentViewModel currentDepartment = vm.DepartmentLookup.First(); DepartmentViewModel toDelete = vm.DepartmentLookup.Skip(1).First(); vm.Department = currentDepartment; vm.DepartmentLookup.Remove(toDelete); Assert.IsFalse(vm.DepartmentLookup.Contains(toDelete), "Department should have been removed from DepartmentLookup."); Assert.AreSame(currentDepartment, vm.Department, "Assigned Department should not have changed."); Assert.IsTrue(ctx.IsObjectTracked(toDelete.Model), "ViewModel is not responsible for deleting departments removed externally."); } }
public void ExternalAddToManagerLookup() { using (FakeEmployeeContext ctx = BuildContextWithData()) { UnitOfWork unit = new UnitOfWork(ctx); EmployeeViewModel vm = BuildViewModel(ctx, unit); EmployeeViewModel currentManager = vm.Manager; EmployeeViewModel newManager = new EmployeeViewModel(new Employee(), vm.ManagerLookup, vm.DepartmentLookup, unit); vm.ManagerLookup.Add(newManager); Assert.IsTrue(vm.ManagerLookup.Contains(newManager), "New department should have been added to ManagerLookup."); Assert.AreSame(currentManager, vm.Manager, "Assigned Manager should not have changed."); Assert.IsFalse(ctx.IsObjectTracked(newManager.Model), "ViewModel is not responsible for adding Employees created externally."); } }
public void ExternalRemoveManagerLookup() { using (FakeEmployeeContext ctx = BuildContextWithData()) { EmployeeViewModel vm = BuildViewModel(ctx); EmployeeViewModel currentManager = vm.ManagerLookup.First(); EmployeeViewModel toDelete = vm.ManagerLookup.Skip(1).First(); vm.Manager = currentManager; vm.ManagerLookup.Remove(toDelete); Assert.IsFalse(vm.ManagerLookup.Contains(toDelete), "Employee should have been removed from ManagerLookup."); Assert.AreSame(currentManager, vm.Manager, "Assigned Manager should not have changed."); Assert.IsTrue(ctx.IsObjectTracked(toDelete.Model), "ViewModel is not responsible for deleting Employees removed externally."); } }
public void ExternalAddToEmployeeCollection() { using (FakeEmployeeContext ctx = BuildContextWithData()) { UnitOfWork unit = new UnitOfWork(ctx); EmployeeWorkspaceViewModel vm = BuildViewModel(ctx, unit); EmployeeViewModel currentEmployee = vm.CurrentEmployee; EmployeeViewModel newEmployee = new EmployeeViewModel(new Employee(), vm.AllEmployees, new ObservableCollection <DepartmentViewModel>(), unit); vm.AllEmployees.Add(newEmployee); Assert.IsTrue(vm.AllEmployees.Contains(newEmployee), "New employee should have been added to AllEmployees."); Assert.AreSame(currentEmployee, vm.CurrentEmployee, "CurrentEmployee should not have changed."); Assert.IsFalse(ctx.IsObjectTracked(newEmployee.Model), "ViewModel is not responsible for adding employees created externally."); } }
/// <summary> /// 验证向工作区和工作单元添加雇员的操作 /// </summary> /// <param name="unitOfWork">应将雇员添加到的上下文</param> /// <param name="vm">要将雇员添加到的工作区</param> private static void TestAddEmployee(FakeEmployeeContext ctx, EmployeeWorkspaceViewModel vm) { List<EmployeeViewModel> originalEmployees = vm.AllEmployees.ToList(); string lastProperty = null; vm.PropertyChanged += (sender, e) => { lastProperty = e.PropertyName; }; Assert.IsTrue(vm.AddEmployeeCommand.CanExecute(null), "Add command should always be enabled."); vm.AddEmployeeCommand.Execute(null); Assert.AreEqual(originalEmployees.Count + 1, vm.AllEmployees.Count, "One new employee should have been added to the AllEmployees property."); Assert.IsFalse(originalEmployees.Contains(vm.CurrentEmployee), "The new employee should be selected."); Assert.IsNotNull(vm.CurrentEmployee, "The new employee should be selected."); Assert.AreEqual("CurrentEmployee", lastProperty, "CurrentEmployee should have raised a PropertyChanged."); Assert.IsTrue(ctx.IsObjectTracked(vm.CurrentEmployee.Model), "The new employee has not been added to the context."); }
public void DeleteContactDetail() { using (FakeEmployeeContext ctx = BuildContextWithData()) { EmployeeViewModel vm = BuildViewModel(ctx); List <ContactDetailViewModel> originalDetails = vm.ContactDetails.ToList(); ContactDetailViewModel toDelete = vm.ContactDetails.First(); vm.CurrentContactDetail = toDelete; vm.DeleteContactDetailCommand.Execute(null); Assert.IsNull(vm.CurrentContactDetail, "No detail should be selected after deleting."); Assert.IsFalse(vm.ContactDetails.Contains(toDelete), "Detail should be removed from ContactDetails property."); Assert.IsFalse(ctx.IsObjectTracked(toDelete), "Detail should be deleted from context."); } }
public void ExternalRemoveFromEmployeeCollection() { using (FakeEmployeeContext ctx = BuildContextWithData()) { UnitOfWork unit = new UnitOfWork(ctx); EmployeeWorkspaceViewModel vm = BuildViewModel(ctx, unit); EmployeeViewModel current = vm.AllEmployees.First(); EmployeeViewModel toDelete = vm.AllEmployees.Skip(1).First(); vm.CurrentEmployee = current; vm.AllEmployees.Remove(toDelete); Assert.IsFalse(vm.AllEmployees.Contains(toDelete), "Employee should have been removed from AllDepartments."); Assert.AreSame(current, vm.CurrentEmployee, "CurrentEmployee should not have changed."); Assert.IsTrue(ctx.IsObjectTracked(toDelete.Model), "ViewModel is not responsible for deleting employees removed externally."); } }
public void AddAddress() { using (FakeEmployeeContext ctx = BuildContextWithData()) { EmployeeViewModel vm = BuildViewModel(ctx); List <ContactDetailViewModel> originalDetails = vm.ContactDetails.ToList(); Assert.IsTrue(vm.AddAddressCommand.CanExecute(null), "AddAddressCommand should always be enabled."); vm.AddAddressCommand.Execute(null); Assert.IsNotNull(vm.CurrentContactDetail, "New address should be selected."); Assert.IsFalse(originalDetails.Contains(vm.CurrentContactDetail), "New address should be selected."); Assert.IsInstanceOfType(vm.CurrentContactDetail, typeof(AddressViewModel), "New contact should be an address."); Assert.IsTrue(ctx.IsObjectTracked(vm.CurrentContactDetail.Model), "New address should have been added to context."); Assert.AreEqual(originalDetails.Count + 1, vm.ContactDetails.Count, "New address should have been added to AllContactDetails property."); } }
public void ExternalRemoveSelectedEmployeeFromCollection() { using (FakeEmployeeContext ctx = BuildContextWithData()) { UnitOfWork unit = new UnitOfWork(ctx); EmployeeWorkspaceViewModel vm = BuildViewModel(ctx, unit); EmployeeViewModel current = vm.CurrentEmployee; string lastProperty = null; vm.PropertyChanged += (sender, e) => { lastProperty = e.PropertyName; }; vm.AllEmployees.Remove(current); Assert.IsFalse(vm.AllEmployees.Contains(current), "Employee should have been removed from AllEmployees."); Assert.IsNull(vm.CurrentEmployee, "CurrentEmployee should have been nulled as it was removed from the collection."); Assert.AreEqual("CurrentEmployee", lastProperty, "CurrentEmployee should have raised a PropertyChanged."); Assert.IsTrue(ctx.IsObjectTracked(current.Model), "ViewModel is not responsible for deleting employees removed externally."); } }
public void ExternalRemoveManagerLookupSelectedManager() { using (FakeEmployeeContext ctx = BuildContextWithData()) { EmployeeViewModel vm = BuildViewModel(ctx); EmployeeViewModel currentManager = vm.Manager; string lastProperty = null; vm.PropertyChanged += (sender, e) => { lastProperty = e.PropertyName; }; vm.ManagerLookup.Remove(currentManager); Assert.IsFalse(vm.ManagerLookup.Contains(currentManager), "Employee should have been removed from ManagerLookup."); Assert.IsNull(vm.Manager, "Assigned Manager should have been nulled as it was removed from the collection."); Assert.AreEqual("Manager", lastProperty, "Manager should have raised a PropertyChanged."); Assert.IsTrue(ctx.IsObjectTracked(currentManager.Model), "ViewModel is not responsible for deleting Employees removed externally."); } }
public void ExternalRemoveDepartmentLookupSelectedDepartment() { using (FakeEmployeeContext ctx = BuildContextWithData()) { EmployeeViewModel vm = BuildViewModel(ctx); DepartmentViewModel currentDepartment = vm.Department; string lastProperty = null; vm.PropertyChanged += (sender, e) => { lastProperty = e.PropertyName; }; vm.DepartmentLookup.Remove(currentDepartment); Assert.IsFalse(vm.DepartmentLookup.Contains(currentDepartment), "Department should have been removed from DepartmentLookup."); Assert.IsNull(vm.Department, "Assigned Department should have been nulled as it was removed from the collection."); Assert.AreEqual("Department", lastProperty, "Department should have raised a PropertyChanged."); Assert.IsTrue(ctx.IsObjectTracked(currentDepartment.Model), "ViewModel is not responsible for deleting departments removed externally."); } }
public void DeleteEmployee() { using (FakeEmployeeContext ctx = BuildContextWithData()) { EmployeeWorkspaceViewModel vm = BuildViewModel(ctx); EmployeeViewModel toDelete = vm.CurrentEmployee; int originalCount = vm.AllEmployees.Count; string lastProperty = null; vm.PropertyChanged += (sender, e) => { lastProperty = e.PropertyName; }; vm.DeleteEmployeeCommand.Execute(null); Assert.AreEqual(originalCount - 1, vm.AllEmployees.Count, "One employee should have been removed from the AllEmployees property."); Assert.IsFalse(vm.AllEmployees.Contains(toDelete), "The selected employee should have been removed."); Assert.IsFalse(ctx.IsObjectTracked(toDelete.Model), "The selected employee has not been removed from the context."); Assert.IsNull(vm.CurrentEmployee, "No employee should be selected after deletion."); Assert.AreEqual("CurrentEmployee", lastProperty, "CurrentEmployee should have raised a PropertyChanged."); } }
public void DeleteDepartment() { using (FakeEmployeeContext ctx = BuildContextWithData()) { DepartmentWorkspaceViewModel vm = BuildViewModel(ctx); vm.CurrentDepartment = vm.AllDepartments.First(); DepartmentViewModel toDelete = vm.CurrentDepartment; int originalCount = vm.AllDepartments.Count; string lastProperty = null; vm.PropertyChanged += (sender, e) => { lastProperty = e.PropertyName; }; vm.DeleteDepartmentCommand.Execute(null); Assert.AreEqual(originalCount - 1, vm.AllDepartments.Count, "One department should have been removed from the AllDepartments property."); Assert.IsFalse(vm.AllDepartments.Contains(toDelete), "The selected department should have been removed."); Assert.IsFalse(ctx.IsObjectTracked(toDelete.Model), "The selected department has not been removed from the UnitOfWork."); Assert.IsNull(vm.CurrentDepartment, "No department should be selected after deletion."); Assert.AreEqual("CurrentDepartment", lastProperty, "CurrentDepartment should have raised a PropertyChanged."); } }
public void IsObjectTracked() { using (FakeEmployeeContext ctx = new FakeEmployeeContext()) { Employee e = new Employee(); Assert.IsFalse(ctx.IsObjectTracked(e), "IsObjectTracked should be false when entity is not in added."); ctx.Employees.AddObject(e); Assert.IsTrue(ctx.IsObjectTracked(e), "IsObjectTracked should be true when entity is added."); Department d = new Department(); Assert.IsFalse(ctx.IsObjectTracked(d), "IsObjectTracked should be false when entity is not in added."); ctx.Departments.AddObject(d); Assert.IsTrue(ctx.IsObjectTracked(d), "IsObjectTracked should be true when entity is added."); ContactDetail c = new Phone(); Assert.IsFalse(ctx.IsObjectTracked(c), "IsObjectTracked should be false when entity is not in added."); ctx.ContactDetails.AddObject(c); Assert.IsTrue(ctx.IsObjectTracked(c), "IsObjectTracked should be true when entity is added."); } }
/// <summary> /// Verifies addition of employee to workspace and unit of work /// </summary> /// <param name="unitOfWork">Context employee should get added to</param> /// <param name="vm">Workspace to add employee to</param> private static void TestAddEmployee(FakeEmployeeContext ctx, EmployeeWorkspaceViewModel vm) { List<EmployeeViewModel> originalEmployees = vm.AllEmployees.ToList(); string lastProperty = null; vm.PropertyChanged += (sender, e) => { lastProperty = e.PropertyName; }; Assert.IsTrue(vm.AddEmployeeCommand.CanExecute(null), "Add command should always be enabled."); vm.AddEmployeeCommand.Execute(null); Assert.AreEqual(originalEmployees.Count + 1, vm.AllEmployees.Count, "One new employee should have been added to the AllEmployees property."); Assert.IsFalse(originalEmployees.Contains(vm.CurrentEmployee), "The new employee should be selected."); Assert.IsNotNull(vm.CurrentEmployee, "The new employee should be selected."); Assert.AreEqual("CurrentEmployee", lastProperty, "CurrentEmployee should have raised a PropertyChanged."); Assert.IsTrue(ctx.IsObjectTracked(vm.CurrentEmployee.Model), "The new employee has not been added to the context."); }