Пример #1
0
        public void AddEmployee_Post_Should_Call_Save_On_Reposiotry_And_Redirect_To_Index()
        {
            var employee = new EmployeeViewModel
                               {
                                   Fname = "Ray",
                                   Lname = "Romano",
                                   Bdate = new DateTime(1980, 10, 01),
                                   Ssn = "123-02-1234",
                                   Dno = 99
                               };
            var validationContext = new ValidationContext(employee, null, null);

            //Create a mock repository
            var mockRepository = new Mock<IEmployeeRepository>();

            var employeeController = new EmployeeController(mockRepository.Object);

            var validationResults = new List<ValidationResult>();
            Validator.TryValidateObject(employee, validationContext, validationResults, true);
            foreach (ValidationResult validationResult in validationResults)
            {
                employeeController.ModelState.AddModelError(validationResult.MemberNames.First(),
                                                            validationResult.ErrorMessage);
            }
            var result = (RedirectToRouteResult) employeeController.AddEmployee(employee);
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof (RedirectToRouteResult));
            Assert.AreEqual("Index", result.RouteValues["action"]);

            mockRepository.Verify(x => x.Save(), Times.AtMostOnce());
        }
Пример #2
0
        public ActionResult AddEmployee(EmployeeViewModel employeeViewModel)
        {
            if (employeeViewModel == null)
                return View("Error");
            if (!ModelState.IsValid)
            {
                return View("AddEmployee", employeeViewModel);
            }

            Employee employee = Mapper.Map<EmployeeViewModel, Employee>(employeeViewModel);
            _repository.Add(employee);
            _repository.Save();
            return RedirectToAction("Index");
        }
Пример #3
0
        public void EditEmployee_Post_Should_Return_EditEmployee_View_When_Dept_No_Is_Not_In_Range()
        {
            var employee = new EmployeeViewModel {Fname = "Ray", Lname = "Romano", Ssn = "123-02-1234", Dno = 1001};
            var validationContext = new ValidationContext(employee, null, null);
            var testRepository = new TestRepository();

            var employeeController = new EmployeeController(testRepository);

            var validationResults = new List<ValidationResult>();
            Validator.TryValidateObject(employee, validationContext, validationResults, true);
            foreach (var validationResult in validationResults)
            {
                employeeController.ModelState.AddModelError(validationResult.MemberNames.First(),
                                                            validationResult.ErrorMessage);
            }

            var result = employeeController.EditEmployee(employee);

            Assert.IsNotNull(result);

            Assert.AreEqual("EditEmployee", ((ViewResult) result).ViewName);
        }
Пример #4
0
 public ViewResult AddEmployee()
 {
     var employee = new EmployeeViewModel();
     return View("AddEmployee", employee);
 }
Пример #5
0
        public ActionResult EditEmployee(EmployeeViewModel employeeViewModel)
        {
            if (employeeViewModel == null)
                return View("Error");

            if (!ModelState.IsValid)
            {
                employeeViewModel.EditMode = true;
                return View("EditEmployee", employeeViewModel);
            }
            else
            {
                var employee = _repository.GetEmployee(employeeViewModel.Ssn);

                if (employee != null)
                {
                    employee.Fname = employeeViewModel.Fname;
                    employee.Lname = employeeViewModel.Lname;
                    employee.Minit = employeeViewModel.Minit.ToString();
                    employee.Bdate = employeeViewModel.Bdate;
                    employee.Dno = employeeViewModel.Dno;
                    employee.Salary = employeeViewModel.Salary;
                    employee.Sex = employeeViewModel.Sex.ToString();

                    employee.Super_ssn = employeeViewModel.Super_ssn;
                    employee.Address = employeeViewModel.Address;

                    //Map ViewModel object to domain object
                    //employee = Mapper.Map<EmployeeViewModel, Employee>(employeeViewModel);

                    employee.Salary = 20000;
                }

                _repository.Save();
            }

            return RedirectToAction("Index");
        }
Пример #6
0
        public void EditEmployee_Post_Should_Save_And_Redirect_ToIndex_When_Data_Is_Valid()
        {
            var employee = new EmployeeViewModel { Fname = "Ray", Lname = "Romano", Ssn = "123-02-1234", Dno = 99 };
            var validationContext = new ValidationContext(employee, null, null);
            var testRepository = new TestRepository();

            var employeeController = new EmployeeController(testRepository);

            var validationResults = new List<ValidationResult>();
            Validator.TryValidateObject(employee, validationContext, validationResults, true);
            foreach (var validationResult in validationResults)
            {
                employeeController.ModelState.AddModelError(validationResult.MemberNames.First(),
                                                            validationResult.ErrorMessage);
            }
            var result = (RedirectToRouteResult)employeeController.EditEmployee(employee);
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
            Assert.AreEqual("Index", result.RouteValues["action"]);
        }
Пример #7
0
        public void EditEmployee_Post_Should_Return_EditEmployee_View_With_Required_Data_Missing()
        {
            var employee = new EmployeeViewModel {Fname = "Ray", Lname = "Romano"/*, Ssn = "123-09-3214"*/};
            var validationContext = new ValidationContext(employee, null, null);
            var testRepository = new TestRepository();

            var employeeController = new EmployeeController(testRepository);

            var validationResults = new List<ValidationResult>();
            Validator.TryValidateObject(employee, validationContext, validationResults);
            foreach (var validationResult in validationResults)
            {
                employeeController.ModelState.AddModelError(validationResult.MemberNames.First(), validationResult.ErrorMessage);
            }

            var result = employeeController.EditEmployee(employee);

            Assert.IsNotNull(result);

            Assert.AreEqual( "EditEmployee", ((ViewResult)result).ViewName  );
        }