Example #1
0
 public void Attach()
 {
     Employee emp = new Employee();
     FakeObjectSet<Employee> set = new FakeObjectSet<Employee>();
     set.Attach(emp);
     Assert.IsTrue(set.Contains(emp), "Attach did not add supplied Employees to public Enumerator.");
 }
        public void GetLongestServingEmployees()
        {
            Employee e1 = new Employee { HireDate = new DateTime(2003, 1, 1) };
            Employee e2 = new Employee { HireDate = new DateTime(2001, 1, 1) };
            Employee e3 = new Employee { HireDate = new DateTime(2000, 1, 1) };
            Employee e4 = new Employee { HireDate = new DateTime(2002, 1, 1) };

            // The following employee verifies GetLongestServingEmployees does not return terminated employees
            Employee e5 = new Employee { HireDate = new DateTime(1999, 1, 1), TerminationDate = DateTime.Today };

            using (FakeEmployeeContext ctx = new FakeEmployeeContext(new Employee[] { e1, e2, e3, e4, e5 }, new Department[] { }))
            {
                EmployeeRepository rep = new EmployeeRepository(ctx);

                // Select a subset
                List<Employee> result = rep.GetLongestServingEmployees(2).ToList();
                Assert.AreEqual(2, result.Count, "Expected two items in result.");
                Assert.AreSame(e3, result[0], "Incorrect item at position 0.");
                Assert.AreSame(e2, result[1], "Incorrect item at position 1.");

                // Select more than are present
                result = rep.GetLongestServingEmployees(50).ToList();
                Assert.AreEqual(4, result.Count, "Expected four items in result.");
            }
        }
Example #3
0
 public void Detach()
 {
     Employee emp = new Employee();
     FakeObjectSet<Employee> set = new FakeObjectSet<Employee>(new Employee[] { emp });
     set.Detach(emp);
     Assert.IsFalse(set.Contains(emp), "Detach did not remove supplied Employees to public Enumerator.");
 }
        public void GetLongestServingEmployees()
        {
            Employee e1 = new Employee { HireDate = new DateTime(2003, 1, 1) };
            Employee e2 = new Employee { HireDate = new DateTime(2001, 1, 1) };
            Employee e3 = new Employee { HireDate = new DateTime(2000, 1, 1) };
            Employee e4 = new Employee { HireDate = new DateTime(2002, 1, 1) };

            // 以下雇员验证 GetLongestServingEmployees 不返回已离职的雇员。
            Employee e5 = new Employee { HireDate = new DateTime(1999, 1, 1), TerminationDate = DateTime.Today };

            using (FakeEmployeeContext ctx = new FakeEmployeeContext(new Employee[] { e1, e2, e3, e4, e5 }, new Department[] { }))
            {
                EmployeeRepository rep = new EmployeeRepository(ctx);

                // 选择子集
                List<Employee> result = rep.GetLongestServingEmployees(2).ToList();
                Assert.AreEqual(2, result.Count, "Expected two items in result.");
                Assert.AreSame(e3, result[0], "Incorrect item at position 0.");
                Assert.AreSame(e2, result[1], "Incorrect item at position 1.");

                // 所选项多于现有项
                result = rep.GetLongestServingEmployees(50).ToList();
                Assert.AreEqual(4, result.Count, "Expected four items in result.");
            }
        }
        /// <summary>
        /// 初始化 BasicEmployeeViewModel 类的新实例。
        /// </summary>
        /// <param name="employee">此 ViewModel 基于的基础雇员</param>
        public BasicEmployeeViewModel(Employee employee)
        {
            if (employee == null)
            {
                throw new ArgumentNullException("employee");
            }

            this.Model = employee;
        }
Example #6
0
        public void AddContactDetailFromDefaultConstructor()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork unit = new UnitOfWork(ctx);
                Employee emp = new Employee();
                unit.AddEmployee(emp);

                ContactDetail cd = new Address();
                unit.AddContactDetail(emp, cd);
                Assert.IsTrue(ctx.ContactDetails.Contains(cd), "ContactDetail was not added to underlying context.");
            }
        }
        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.");
            }
        }
Example #8
0
        public void AddContactDetailToEmployeeOutsideUnitOfWork()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork unit = new UnitOfWork(ctx);
                Employee emp = new Employee();
                ContactDetail detail = new Email();

                try
                {
                    unit.AddContactDetail(emp, detail);
                    Assert.Fail("Adding a contact detail to an employee outside the Unit of Work did not throw.");
                }
                catch (InvalidOperationException ex)
                {
                    Assert.AreEqual("The supplied Employee is not part of this Unit of Work.", ex.Message);
                }
            }
        }
        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 #10
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 GetAllEmployees()
        {
            Employee e1 = new Employee();
            Employee e2 = new Employee();
            Employee e3 = new Employee();

            using (FakeEmployeeContext ctx = new FakeEmployeeContext(new Employee[] { e1, e2, e3 }, new Department[] { }))
            {
                EmployeeRepository rep = new EmployeeRepository(ctx);

                IEnumerable<Employee> result = rep.GetAllEmployees();

                Assert.IsNotInstanceOfType(
                    result,
                    typeof(IQueryable),
                    "Repositories should not return IQueryable as this allows modification of the query that gets sent to the store. ");

                Assert.AreEqual(3, result.Count());
                Assert.IsTrue(result.Contains(e1));
                Assert.IsTrue(result.Contains(e2));
                Assert.IsTrue(result.Contains(e3));
            }
        }
        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.");
            }
        }
 private static int GetTenure(Employee employee)
 {
     // NOTE: The body for this method is included to facilitate running against in-memory fakes
     //       EF does not require an implementation, see notes in GetLongestServingEmployees()
     DateTime endDate = employee.TerminationDate ?? DateTime.Today;
     return endDate.Subtract(employee.HireDate).Days / 365;
 }
Example #14
0
 public void InitializationWithTestData()
 {
     Employee emp = new Employee();
     FakeObjectSet<Employee> set = new FakeObjectSet<Employee>(new Employee[] { emp });
     Assert.IsTrue(set.Contains(emp), "Constructor did not add supplied Employees to public Enumerator.");
 }
        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.");
            }
        }
Example #16
0
        /// <summary>
        /// Builds a FakeUnitOfWork with realistic seeded data
        /// </summary>
        /// <returns>Context seeded with data</returns>
        public static FakeEmployeeContext BuildFakeSession()
        {
            Department sales = new Department { DepartmentName = "Sales", DepartmentCode = "SALES", LastAudited = new DateTime(2009, 4, 1) };
            Department corporate = new Department { DepartmentName = "Corporate", DepartmentCode = "CORP" };
            Department purchasing = new Department { DepartmentName = "Purchasing", DepartmentCode = "PURCH" };
            Department marketing = new Department { DepartmentName = "Marketing", DepartmentCode = "MARK", LastAudited = new DateTime(2009, 1, 1) };

            Employee e1 = new Employee { Title = "Mr.", FirstName = "Nancy", LastName = "Davolio", Position = "Sales Representative", HireDate = new DateTime(1992, 5, 1), BirthDate = new DateTime(1948, 12, 8), Department = sales, ContactDetails = new List<ContactDetail>() };
            Employee e2 = new Employee { Title = "Dr.", FirstName = "Andrew", LastName = "Fuller", Position = "Vice President", HireDate = new DateTime(1992, 8, 14), BirthDate = new DateTime(1952, 2, 19), Department = corporate, ContactDetails = new List<ContactDetail>() };
            Employee e3 = new Employee { Title = "Ms.", FirstName = "Janet", LastName = "Leverling", Position = "Sales Representative", HireDate = new DateTime(1992, 4, 1), BirthDate = new DateTime(1963, 8, 30), Department = sales, ContactDetails = new List<ContactDetail>() };
            Employee e4 = new Employee { Title = "Mrs.", FirstName = "Margaret", LastName = "Peacock", Position = "Sales Representative", HireDate = new DateTime(1993, 5, 3), BirthDate = new DateTime(1937, 9, 19), Department = sales, ContactDetails = new List<ContactDetail>() };
            Employee e5 = new Employee { Title = "Mr.", FirstName = "Steven", LastName = "Buchanan", Position = "Sales Manager", HireDate = new DateTime(1993, 10, 17), BirthDate = new DateTime(1955, 3, 4), Department = sales, ContactDetails = new List<ContactDetail>() };
            Employee e6 = new Employee { Title = "Mr.", FirstName = "Michael", LastName = "Suyama", Position = "Sales Representative", HireDate = new DateTime(1993, 10, 17), BirthDate = new DateTime(1963, 7, 2), Department = sales, ContactDetails = new List<ContactDetail>() };
            Employee e7 = new Employee { Title = "Mr.", FirstName = "Robert", LastName = "King", Position = "Sales Representative", HireDate = new DateTime(1994, 1, 2), BirthDate = new DateTime(1960, 5, 29), Department = sales, ContactDetails = new List<ContactDetail>() };
            Employee e8 = new Employee { Title = "Ms.", FirstName = "Callahan", LastName = "Laura", Position = "Inside Sales Coordinator", HireDate = new DateTime(1994, 3, 5), BirthDate = new DateTime(1958, 1, 9), Department = sales, ContactDetails = new List<ContactDetail>() };
            Employee e9 = new Employee { Title = "Ms.", FirstName = "Anne", LastName = "Dodsworth", Position = "Sales Representative", HireDate = new DateTime(1994, 11, 15), BirthDate = new DateTime(1996, 1, 27), Department = sales, ContactDetails = new List<ContactDetail>() };

            e1.ContactDetails.Add(new Address { Usage = "Home", LineOne = "507 - 20th Ave. E.  Apt. 2A", City = "Seattle", State = "WA", ZipCode = "98122", Country = "USA" });
            e2.ContactDetails.Add(new Address { Usage = "Home", LineOne = "908 W. Capital Wa", City = "Tacoma", State = "WA", ZipCode = "98401", Country = "USA" });
            e3.ContactDetails.Add(new Address { Usage = "Business", LineOne = "722 Moss Bay Blvd.", City = "Kirkland", State = "WA", ZipCode = "98033", Country = "USA" });
            e4.ContactDetails.Add(new Address { Usage = "Business", LineOne = "4110 Old Redmond Rd.", City = "Redmond", State = "WA", ZipCode = "98052", Country = "USA" });
            e5.ContactDetails.Add(new Address { Usage = "Business", LineOne = "14 Garrett Hill", City = "London", ZipCode = "SW1 8JR", Country = "UK" });
            e6.ContactDetails.Add(new Address { Usage = "Business", LineOne = "Coventry House  Miner Rd.", City = "London", ZipCode = "EC2 7JR", Country = "UK" });
            e7.ContactDetails.Add(new Address { Usage = "Mailing", LineOne = "Edgeham Hollow  Winchester Way", City = "London", ZipCode = "RG1 9SP", Country = "UK" });
            e8.ContactDetails.Add(new Address { Usage = "Mailing", LineOne = "4726 - 11th Ave. N.E.", City = "Seattle", State = "WA", ZipCode = "98105", Country = "USA" });
            e9.ContactDetails.Add(new Address { Usage = "Mailing", LineOne = "7 Houndstooth Rd.", City = "London", ZipCode = "WG2 7LT", Country = "UK" });

            e1.ContactDetails.Add(new Phone { Usage = "Business", Number = "(206) 555-9857", Extension = "5467" });
            e1.ContactDetails.Add(new Phone { Usage = "Cell", Number = "(71) 555-7773" });
            e2.ContactDetails.Add(new Phone { Usage = "Business", Number = "(206) 555-9482", Extension = "3457" });
            e2.ContactDetails.Add(new Phone { Usage = "Home", Number = "(71) 555-5598" });
            e3.ContactDetails.Add(new Phone { Usage = "Business", Number = "(206) 555-3412", Extension = "3355" });
            e3.ContactDetails.Add(new Phone { Usage = "Home", Number = "(206) 555-1189" });
            e4.ContactDetails.Add(new Phone { Usage = "Business", Number = "(206) 555-8122", Extension = "5176" });
            e4.ContactDetails.Add(new Phone { Usage = "Home", Number = "(71) 555-4444" });
            e5.ContactDetails.Add(new Phone { Usage = "Business", Number = "(71) 555-4848", Extension = "3453" });
            e6.ContactDetails.Add(new Phone { Usage = "Cell", Number = "(71) 555-7773" });
            e7.ContactDetails.Add(new Phone { Usage = "Cell", Number = "(71) 555-5598" });
            e8.ContactDetails.Add(new Phone { Usage = "Cell", Number = "(206) 555-1189" });
            e9.ContactDetails.Add(new Phone { Usage = "Cell", Number = "(71) 555-4444" });

            e1.ContactDetails.Add(new Email { Usage = "Business", Address = "*****@*****.**" });
            e2.ContactDetails.Add(new Email { Usage = "Business", Address = "*****@*****.**" });
            e3.ContactDetails.Add(new Email { Usage = "Business", Address = "*****@*****.**" });
            e4.ContactDetails.Add(new Email { Usage = "Business", Address = "*****@*****.**" });
            e5.ContactDetails.Add(new Email { Usage = "Business", Address = "*****@*****.**" });
            e6.ContactDetails.Add(new Email { Usage = "Personal", Address = "*****@*****.**" });
            e7.ContactDetails.Add(new Email { Usage = "Personal", Address = "*****@*****.**" });
            e8.ContactDetails.Add(new Email { Usage = "Personal", Address = "*****@*****.**" });
            e9.ContactDetails.Add(new Email { Usage = "Personal", Address = "*****@*****.**" });

            e1.Manager = e2;
            e3.Manager = e2;
            e4.Manager = e2;
            e5.Manager = e2;
            e6.Manager = e5;
            e7.Manager = e5;
            e8.Manager = e2;
            e9.Manager = e5;

            FakeEmployeeContext context = new FakeEmployeeContext(
                new List<Employee> { e1, e2, e3, e4, e5, e6, e7, e8, e9 },
                new List<Department> { sales, corporate, purchasing, marketing });
            return context;
        }
Example #17
0
        public void AddEmployee()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork unit = new UnitOfWork(ctx);

                Employee emp = new Employee();
                unit.AddEmployee(emp);
                Assert.IsTrue(ctx.Employees.Contains(emp), "Employee was not added to underlying context.");
            }
        }
Example #18
0
        public void RemoveEmployeeWithReports()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork unit = new UnitOfWork(ctx);
                Employee emp = new Employee();
                Employee man = new Employee();
                unit.AddEmployee(emp);
                unit.AddEmployee(man);
                emp.Manager = man;

                unit.RemoveEmployee(man);
                Assert.IsFalse(ctx.Employees.Contains(man), "Employee was not removed from underlying context.");
                Assert.AreEqual(0, man.Reports.Count, "Employee was not removed from managers reports.");
                Assert.IsNull(emp.Manager, "Manager property on Employee was not cleared.");
            }
        }
Example #19
0
        public void RemoveEmployee()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork unit = new UnitOfWork(ctx);
                Employee emp = new Employee();
                unit.AddEmployee(emp);

                unit.RemoveEmployee(emp);
                Assert.IsFalse(ctx.Employees.Contains(emp), "Employee was not removed from underlying context.");
            }
        }
Example #20
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.");
            }
        }
        /// <summary>
        /// Creates a fake context with seed data
        /// </summary>
        /// <returns>The fake context</returns>
        private static FakeEmployeeContext BuildContextWithData()
        {
            Department d1 = new Department();
            Department d2 = new Department();

            Employee e1 = new Employee { Department = d1 };
            Employee e2 = new Employee { Department = d1 };

            e1.Manager = e2;

            e1.ContactDetails.Add(new Phone());
            e1.ContactDetails.Add(new Email());
            e1.ContactDetails.Add(new Address());
            e2.ContactDetails.Add(new Phone());

            return new FakeEmployeeContext(new Employee[] { e1, e2 }, new Department[] { d1, d2 });
        }
Example #22
0
 /// <summary>
 /// 用于向 Employees EntitySet 添加新对象的方法,已弃用。请考虑改用关联的 ObjectSet&lt;T&gt; 属性的 .Add 方法。
 /// </summary>
 public void AddToEmployees(Employee employee)
 {
     base.AddObject("Employees", employee);
 }
Example #23
0
        public void RemoveDepartmentWithEmployees()
        {
            using (FakeEmployeeContext ctx = new FakeEmployeeContext())
            {
                UnitOfWork unit = new UnitOfWork(ctx);
                Department dep = new Department();
                Employee emp = new Employee();
                unit.AddDepartment(dep);
                unit.AddEmployee(emp);
                emp.Department = dep;

                unit.RemoveDepartment(dep);
                Assert.IsFalse(ctx.Departments.Contains(dep), "Department was not removed from underlying context.");
                Assert.IsNull(emp.Department, "Employee.Department property has not been nulled when deleting department.");
                Assert.IsNull(emp.DepartmentId, "Employee.DepartmentId property has not been nulled when deleting department.");
                Assert.AreEqual(0, dep.Employees.Count, "Department.Employees collection was not cleared when deleting department.");
            }
        }