/// <summary> /// 创建基于虚设上下文的 ViewModel /// </summary> /// <param name="ctx">视图模型基于的上下文</param> /// <returns>新的 ViewModel</returns> private static DepartmentWorkspaceViewModel BuildViewModel(FakeEmployeeContext ctx) { UnitOfWork unit = new UnitOfWork(ctx); ObservableCollection<DepartmentViewModel> departments = new ObservableCollection<DepartmentViewModel>(ctx.Departments.Select(d => new DepartmentViewModel(d))); DepartmentWorkspaceViewModel vm = new DepartmentWorkspaceViewModel(departments, unit); return vm; }
/// <summary> /// 验证向工作区和工作单元添加部门的操作 /// </summary> /// <param name="ctx">应将部门添加到的上下文</param> /// <param name="vm">要将部门添加到的工作区</param> private static void TestAddDepartment(FakeEmployeeContext ctx, DepartmentWorkspaceViewModel vm) { List<DepartmentViewModel> originalDepartments = vm.AllDepartments.ToList(); string lastProperty = null; vm.PropertyChanged += (sender, e) => { lastProperty = e.PropertyName; }; Assert.IsTrue(vm.AddDepartmentCommand.CanExecute(null), "Add command should always be enabled."); vm.AddDepartmentCommand.Execute(null); Assert.AreEqual(originalDepartments.Count + 1, vm.AllDepartments.Count, "One new department should have been added to the AllDepartments property."); Assert.IsFalse(originalDepartments.Contains(vm.CurrentDepartment), "The new department should be selected."); Assert.IsNotNull(vm.CurrentDepartment, "The new department should be selected."); Assert.AreEqual("CurrentDepartment", lastProperty, "CurrentDepartment should have raised a PropertyChanged."); Assert.IsTrue(ctx.IsObjectTracked(vm.CurrentDepartment.Model), "The new department has not been added to the context."); }
public void InitializeWithEmptyData() { using (FakeEmployeeContext ctx = new FakeEmployeeContext()) { UnitOfWork unit = new UnitOfWork(ctx); ObservableCollection<DepartmentViewModel> departments = new ObservableCollection<DepartmentViewModel>(); DepartmentWorkspaceViewModel vm = new DepartmentWorkspaceViewModel(departments, unit); Assert.IsNull(vm.CurrentDepartment, "Current department should not be set if there are no department."); Assert.AreSame(departments, vm.AllDepartments, "ViewModel should expose the same instance of the collection so that changes outside the ViewModel are reflected."); Assert.IsNotNull(vm.AddDepartmentCommand, "AddDepartmentCommand should be initialized"); Assert.IsNotNull(vm.DeleteDepartmentCommand, "DeleteDepartmentCommand should be initialized"); } }