public void ModelChangesFlowToProperties() { // 测试 ViewModel 返回模型的当前值 Department dep = new Department { DepartmentName = "DepartmentName", DepartmentCode = "DepartmentCode" }; DepartmentViewModel vm = new DepartmentViewModel(dep); vm.DepartmentName = "DepartmentName_NEW"; Assert.AreEqual("DepartmentName_NEW", dep.DepartmentName, "DepartmentName property is not fetching the value from the model."); vm.DepartmentCode = "DepartmentCode_NEW"; Assert.AreEqual("DepartmentCode_NEW", dep.DepartmentCode, "DepartmentCode property is not fetching the value from the model."); }
public void PropertyGetAndSet() { // 测试初始属性显示在 ViewModel 中 Department dep = new Department { DepartmentName = "DepartmentName", DepartmentCode = "DepartmentCode" }; DepartmentViewModel vm = new DepartmentViewModel(dep); Assert.AreEqual(dep, vm.Model, "Bound object property did not return object from model."); Assert.AreEqual("DepartmentName", vm.DepartmentName, "DepartmentName property did not return value from model."); Assert.AreEqual("DepartmentCode", vm.DepartmentCode, "DepartmentCode property did not return value from model."); // 更改属性的测试将更新模型并引发 PropertyChanged string lastProperty; vm.PropertyChanged += (sender, e) => { lastProperty = e.PropertyName; }; lastProperty = null; vm.DepartmentName = "DepartmentName_NEW"; Assert.AreEqual("DepartmentName", lastProperty, "Setting DepartmentName property did not raise correct PropertyChanged event."); Assert.AreEqual("DepartmentName_NEW", dep.DepartmentName, "Setting DepartmentName property did not update model."); lastProperty = null; vm.DepartmentCode = "DepartmentCode_NEW"; Assert.AreEqual("DepartmentCode", lastProperty, "Setting DepartmentName property did not raise correct PropertyChanged event."); Assert.AreEqual("DepartmentCode_NEW", dep.DepartmentCode, "Setting DepartmentCode property did not update model."); }
public void PropertyGetAndSet() { // Test initial properties are surfaced in ViewModel Department dep = new Department { DepartmentName = "DepartmentName", DepartmentCode = "DepartmentCode" }; DepartmentViewModel vm = new DepartmentViewModel(dep); Assert.AreEqual(dep, vm.Model, "Bound object property did not return object from model."); Assert.AreEqual("DepartmentName", vm.DepartmentName, "DepartmentName property did not return value from model."); Assert.AreEqual("DepartmentCode", vm.DepartmentCode, "DepartmentCode property did not return value from model."); // Test changing properties updates Model and raises PropertyChanged string lastProperty; vm.PropertyChanged += (sender, e) => { lastProperty = e.PropertyName; }; lastProperty = null; vm.DepartmentName = "DepartmentName_NEW"; Assert.AreEqual("DepartmentName", lastProperty, "Setting DepartmentName property did not raise correct PropertyChanged event."); Assert.AreEqual("DepartmentName_NEW", dep.DepartmentName, "Setting DepartmentName property did not update model."); lastProperty = null; vm.DepartmentCode = "DepartmentCode_NEW"; Assert.AreEqual("DepartmentCode", lastProperty, "Setting DepartmentName property did not raise correct PropertyChanged event."); Assert.AreEqual("DepartmentCode_NEW", dep.DepartmentCode, "Setting DepartmentCode property did not update model."); }
public void ReferencesGetAndSet() { // Scalar properties are inherited from BasicEmployeeViewModel and are already tested Department d1 = new Department(); Department d2 = new Department(); Employee e1 = new Employee(); Employee e2 = new Employee(); Employee employee = new Employee { Department = d1, Manager = e1 }; employee.ContactDetails.Add(new Phone()); employee.ContactDetails.Add(new Email()); using (FakeEmployeeContext ctx = new FakeEmployeeContext(new Employee[] { e1, e2, employee }, new Department[] { d1, d2 })) { UnitOfWork unit = new UnitOfWork(ctx); DepartmentViewModel dvm1 = new DepartmentViewModel(d1); DepartmentViewModel dvm2 = new DepartmentViewModel(d2); ObservableCollection<DepartmentViewModel> departments = new ObservableCollection<DepartmentViewModel> { dvm1, dvm2 }; ObservableCollection<EmployeeViewModel> employees = new ObservableCollection<EmployeeViewModel>(); EmployeeViewModel evm1 = new EmployeeViewModel(e1, employees, departments, unit); EmployeeViewModel evm2 = new EmployeeViewModel(e2, employees, departments, unit); EmployeeViewModel employeeViewModel = new EmployeeViewModel(employee, employees, departments, unit); employees.Add(evm1); employees.Add(evm2); employees.Add(employeeViewModel); // Test initial references are surfaced in ViewModel Assert.AreEqual(evm1, employeeViewModel.Manager, "ViewModel did not return ViewModel representing current manager."); Assert.AreEqual(e1, employeeViewModel.Manager.Model, "ViewModel did not return ViewModel representing current manager."); Assert.AreEqual(dvm1, employeeViewModel.Department, "ViewModel did not return ViewModel representing current department."); Assert.AreEqual(d1, employeeViewModel.Department.Model, "ViewModel did not return ViewModel representing current department."); Assert.AreEqual(2, employeeViewModel.ContactDetails.Count, "Contact details have not been populated on ViewModel."); // Test changing properties updates Model and raises PropertyChanged string lastProperty; employeeViewModel.PropertyChanged += (sender, e) => { lastProperty = e.PropertyName; }; lastProperty = null; employeeViewModel.Department = dvm2; Assert.AreEqual("Department", lastProperty, "Setting Department property did not raise correct PropertyChanged event."); Assert.AreEqual(d2, employee.Department, "Setting Department property in ViewModel is not reflected in Model."); lastProperty = null; employeeViewModel.Manager = evm2; Assert.AreEqual("Manager", lastProperty, "Setting Manager property did not raise correct PropertyChanged event."); Assert.AreEqual(e2, employee.Manager, "Setting Manager property in ViewModel is not reflected in Model."); // Test ViewModel returns current value from model employee.Manager = e1; Assert.AreEqual(evm1, employeeViewModel.Manager, "ViewModel did not return correct manager when model was updated outside of ViewModel."); employee.Department = d1; Assert.AreEqual(dvm1, employeeViewModel.Department, "ViewModel did not return correct department when model was updated outside of ViewModel."); // Test ViewModel returns current value from model when set to null employee.Manager = null; Assert.AreEqual(null, employeeViewModel.Manager, "ViewModel did not return correct manager when model was updated outside of ViewModel."); employee.Department = null; Assert.AreEqual(null, employeeViewModel.Department, "ViewModel did not return correct department when model was updated outside of ViewModel."); } }
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."); } }
/// <summary> /// Handles addition a new department to the workspace and model /// </summary> private void AddDepartment() { Department dep = this.unitOfWork.CreateObject<Department>(); this.unitOfWork.AddDepartment(dep); DepartmentViewModel vm = new DepartmentViewModel(dep); this.AllDepartments.Add(vm); this.CurrentDepartment = vm; }
/// <summary> /// 处理删除当前部门的操作 /// </summary> private void DeleteCurrentDepartment() { this.unitOfWork.RemoveDepartment(this.CurrentDepartment.Model); this.AllDepartments.Remove(this.CurrentDepartment); this.CurrentDepartment = null; }
public void ReferencesGetAndSet() { // Scalar 属性继承自 BasicEmployeeViewModel 并且已经测试 Department d1 = new Department(); Department d2 = new Department(); Employee e1 = new Employee(); Employee e2 = new Employee(); Employee employee = new Employee { Department = d1, Manager = e1 }; employee.ContactDetails.Add(new Phone()); employee.ContactDetails.Add(new Email()); using (FakeEmployeeContext ctx = new FakeEmployeeContext(new Employee[] { e1, e2, employee }, new Department[] { d1, d2 })) { UnitOfWork unit = new UnitOfWork(ctx); DepartmentViewModel dvm1 = new DepartmentViewModel(d1); DepartmentViewModel dvm2 = new DepartmentViewModel(d2); ObservableCollection<DepartmentViewModel> departments = new ObservableCollection<DepartmentViewModel> { dvm1, dvm2 }; ObservableCollection<EmployeeViewModel> employees = new ObservableCollection<EmployeeViewModel>(); EmployeeViewModel evm1 = new EmployeeViewModel(e1, employees, departments, unit); EmployeeViewModel evm2 = new EmployeeViewModel(e2, employees, departments, unit); EmployeeViewModel employeeViewModel = new EmployeeViewModel(employee, employees, departments, unit); employees.Add(evm1); employees.Add(evm2); employees.Add(employeeViewModel); // 测试初始引用显示在 ViewModel 中 Assert.AreEqual(evm1, employeeViewModel.Manager, "ViewModel did not return ViewModel representing current manager."); Assert.AreEqual(e1, employeeViewModel.Manager.Model, "ViewModel did not return ViewModel representing current manager."); Assert.AreEqual(dvm1, employeeViewModel.Department, "ViewModel did not return ViewModel representing current department."); Assert.AreEqual(d1, employeeViewModel.Department.Model, "ViewModel did not return ViewModel representing current department."); Assert.AreEqual(2, employeeViewModel.ContactDetails.Count, "Contact details have not been populated on ViewModel."); // 更改属性的测试将更新模型并引发 PropertyChanged string lastProperty; employeeViewModel.PropertyChanged += (sender, e) => { lastProperty = e.PropertyName; }; lastProperty = null; employeeViewModel.Department = dvm2; Assert.AreEqual("Department", lastProperty, "Setting Department property did not raise correct PropertyChanged event."); Assert.AreEqual(d2, employee.Department, "Setting Department property in ViewModel is not reflected in Model."); lastProperty = null; employeeViewModel.Manager = evm2; Assert.AreEqual("Manager", lastProperty, "Setting Manager property did not raise correct PropertyChanged event."); Assert.AreEqual(e2, employee.Manager, "Setting Manager property in ViewModel is not reflected in Model."); // 测试 ViewModel 返回模型的当前值 employee.Manager = e1; Assert.AreEqual(evm1, employeeViewModel.Manager, "ViewModel did not return correct manager when model was updated outside of ViewModel."); employee.Department = d1; Assert.AreEqual(dvm1, employeeViewModel.Department, "ViewModel did not return correct department when model was updated outside of ViewModel."); // 当设置为 null 时,测试 ViewModel 返回模型的当前值 employee.Manager = null; Assert.AreEqual(null, employeeViewModel.Manager, "ViewModel did not return correct manager when model was updated outside of ViewModel."); employee.Department = null; Assert.AreEqual(null, employeeViewModel.Department, "ViewModel did not return correct department when model was updated outside of ViewModel."); } }
/// <summary> /// 生成一个包含设为种子的数据的虚设上下文 /// </summary> /// <returns>已填充的上下文</returns> private static FakeEmployeeContext BuildContextWithData() { Department d1 = new Department(); Department d2 = new Department(); DepartmentViewModel dvm1 = new DepartmentViewModel(d1); DepartmentViewModel dvm2 = new DepartmentViewModel(d2); return new FakeEmployeeContext(new Employee[] { }, new Department[] { d1, d2 }); }