Example #1
0
        /// <summary>
        /// 初始化 PhoneViewModel 类的新实例。
        /// </summary>
        /// <param name="detail">此 ViewModel 基于的基础电话</param>
        public PhoneViewModel(Phone detail)
        {
            if (detail == null)
            {
                throw new ArgumentNullException("detail");
            }

            this.phone = detail;
        }
        public void ModelChangesFlowToProperties()
        {
            // Test ViewModel returns current value from model
            Phone ph = new Phone { Number = "NUMBER", Extension = "EXTENSION" };
            PhoneViewModel vm = new PhoneViewModel(ph);

            ph.Number = "NEW_NUMBER";
            ph.Extension = "NEW_EXTENSION";
            Assert.AreEqual("NEW_NUMBER", vm.Number, "Number property is not fetching the value from the model.");
            Assert.AreEqual("NEW_EXTENSION", vm.Extension, "Extension property is not fetching the value from the model.");
        }
        public void InitializationWithSuppliedCollections()
        {
            Department dep = new Department();
            ContactDetail det = new Phone();
            Employee emp = new Employee { ContactDetails = new List<ContactDetail> { det } };

            using (FakeEmployeeContext ctx = new FakeEmployeeContext(new Employee[] { emp }, new Department[] { dep }))
            {
                Assert.IsTrue(ctx.Employees.Contains(emp), "Constructor did not add supplied Employees to public ObjectSet.");
                Assert.IsTrue(ctx.Departments.Contains(dep), "Constructor did not add supplied Departments to public ObjectSet.");
                Assert.IsTrue(ctx.ContactDetails.Contains(det), "Constructor did not add supplied ContactDetails to public ObjectSet.");
            }
        }
        public void BuildViewModel()
        {
            Phone p = new Phone();
            Email e = new Email();
            Address a = new Address();

            var pvm = ContactDetailViewModel.BuildViewModel(p);
            Assert.IsInstanceOfType(pvm, typeof(PhoneViewModel), "Factory method created wrong ViewModel type.");
            Assert.AreEqual(p, pvm.Model, "Underlying model object on ViewModel is not correct.");

            var evm = ContactDetailViewModel.BuildViewModel(e);
            Assert.IsInstanceOfType(evm, typeof(EmailViewModel), "Factory method created wrong ViewModel type.");
            Assert.AreEqual(e, evm.Model, "Underlying model object on ViewModel is not correct.");

            var avm = ContactDetailViewModel.BuildViewModel(a);
            Assert.IsInstanceOfType(avm, typeof(AddressViewModel), "Factory method created wrong ViewModel type.");
            Assert.AreEqual(a, avm.Model, "Underlying model object on ViewModel is not correct.");
        }
        public void IsObjectTracked()
        {
            using (EmployeeEntities ctx = new EmployeeEntities())
            {
                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.");
            }
        }
Example #6
0
        public void AddContactDetailAlreadyInUnitOfWork()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork unit = new UnitOfWork(ctx);

                Employee emp = new Employee();
                ContactDetail detail = new Phone();
                unit.AddEmployee(emp);
                unit.AddContactDetail(emp, detail);

                try
                {
                    unit.AddContactDetail(emp, detail);
                    Assert.Fail("Adding an ContactDetail that was already added did not throw.");
                }
                catch (InvalidOperationException ex)
                {
                    Assert.AreEqual("The supplied Phone is already part of this Unit of Work.", ex.Message);
                }
            }
        }
        public void PropertyGetAndSet()
        {
            // Test initial properties are surfaced in ViewModel
            Phone ph = new Phone { Number = "NUMBER", Extension = "EXTENSION" };
            PhoneViewModel vm = new PhoneViewModel(ph);
            Assert.AreEqual(ph, vm.Model, "Bound object property did not return object from model.");
            Assert.AreEqual(ph.ValidUsageValues, vm.ValidUsageValues, "ValidUsageValues property did not return value from model.");
            Assert.AreEqual("NUMBER", vm.Number, "Number property did not return value from model.");
            Assert.AreEqual("EXTENSION", vm.Extension, "Extension 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.Number = "NEW_NUMBER";
            Assert.AreEqual("Number", lastProperty, "Setting Number property did not raise correct PropertyChanged event.");
            Assert.AreEqual("NEW_NUMBER", ph.Number, "Setting Number property did not update model.");

            lastProperty = null;
            vm.Extension = "NEW_EXTENSION";
            Assert.AreEqual("Extension", lastProperty, "Setting Extension property did not raise correct PropertyChanged event.");
            Assert.AreEqual("NEW_EXTENSION", ph.Extension, "Setting Extension property did not update model.");
        }
Example #8
0
        public void RemoveContactDetail()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork unit = new UnitOfWork(ctx);

                Employee emp = new Employee();
                ContactDetail detail = new Phone();
                unit.AddEmployee(emp);
                unit.AddContactDetail(emp, detail);

                unit.RemoveContactDetail(emp, detail);
                Assert.IsFalse(ctx.ContactDetails.Contains(detail), "ContactDetail was not removed from underlying context.");
                Assert.IsFalse(
                    emp.ContactDetails.Contains(detail),
                    "ContactDetail is still in collection on Employee after being removed via Unit of Work.");
            }
        }