public async Task Edit_Post_ModelIsInvalid_SetViewBagEmployees()
        {
            Mock <BusinessTripService> bmock = new Mock <BusinessTripService>();

            bmock.Setup(m => m.EditAsync(It.IsAny <BusinessTripDTO>())).Throws(new ValidationException("", ""));
            Mock <EmployeeService> emock = new Mock <EmployeeService>();

            emock.Setup(m => m.GetAllAsync()).ReturnsAsync(new EmployeeDTO[] {
                new EmployeeDTO()
                {
                    Id = 2, LastName = "Петров", FirstName = "Петр", Patronymic = "Петрович", Post = new PostDTO {
                        Department = new DepartmentDTO {
                            DepartmentName = "IT"
                        }
                    }
                }
            });
            BusinessTripController controller = GetNewBusinessTripController(bmock.Object, emock.Object);

            ViewResult result = (await controller.Edit(new BusinessTripViewModel())) as ViewResult;

            SelectListItem item = (result.ViewBag.Employees as SelectList).FirstOrDefault();

            Assert.AreEqual("Петров Петр Петрович [IT]", item.Text);
            Assert.AreEqual("2", item.Value);
        }
        public async Task Edit_Post_ModelIsValid_RedirectToIndex()
        {
            Mock <BusinessTripService> mock       = new Mock <BusinessTripService>();
            BusinessTripController     controller = GetNewBusinessTripController(mock.Object, null);

            RedirectToRouteResult result = (await controller.Edit(new BusinessTripViewModel())) as RedirectToRouteResult;

            Assert.AreEqual("Index", result.RouteValues["action"]);
        }
        public async Task Edit_Get_ModelIsValid_AsksForEditView()
        {
            Mock <BusinessTripService> bmock      = new Mock <BusinessTripService>();
            Mock <EmployeeService>     emock      = new Mock <EmployeeService>();
            BusinessTripController     controller = GetNewBusinessTripController(bmock.Object, emock.Object);

            ViewResult result = (await controller.Edit(1)) as ViewResult;

            Assert.AreEqual("Edit", result.ViewName);
        }
        public async Task Edit_Post_ModelIsInvalid_AsksForEditView()
        {
            Mock <BusinessTripService> bmock = new Mock <BusinessTripService>();

            bmock.Setup(m => m.EditAsync(It.IsAny <BusinessTripDTO>())).Throws(new ValidationException("", ""));
            Mock <EmployeeService> emock      = new Mock <EmployeeService>();
            BusinessTripController controller = GetNewBusinessTripController(bmock.Object, emock.Object);

            ViewResult result = (await controller.Edit(new BusinessTripViewModel())) as ViewResult;

            Assert.AreEqual("Edit", result.ViewName);
        }
        public async Task Edit_Get_ModelIsInvalid_AsksForErrorView()
        {
            Mock <BusinessTripService> bmock = new Mock <BusinessTripService>();

            bmock.Setup(m => m.FindByIdAsync(It.IsAny <int?>())).Throws(new ValidationException("FindByIdAsync method throws Exception", ""));
            Mock <EmployeeService> emock      = new Mock <EmployeeService>();
            BusinessTripController controller = GetNewBusinessTripController(bmock.Object, emock.Object);

            ViewResult result = (await controller.Edit(1)) as ViewResult;

            Assert.AreEqual("Error", result.ViewName);
        }
        public async Task Edit_Get_ModelIsInvalid_RetrievesExceptionMessageFromModel()
        {
            Mock <BusinessTripService> bmock = new Mock <BusinessTripService>();

            bmock.Setup(m => m.FindByIdAsync(It.IsAny <int?>())).Throws(new ValidationException("FindByIdAsync method throws Exception", ""));
            Mock <EmployeeService> emock      = new Mock <EmployeeService>();
            BusinessTripController controller = GetNewBusinessTripController(bmock.Object, emock.Object);

            ViewResult result = (await controller.Edit(1)) as ViewResult;

            string[] model = result.ViewData.Model as string[];
            Assert.AreEqual("FindByIdAsync method throws Exception", model[0]);
        }
        public async Task Edit_Post_ModelIsInvalid_RetrievesBusinessTripFromModel()
        {
            Mock <BusinessTripService> bmock = new Mock <BusinessTripService>();

            bmock.Setup(m => m.EditAsync(It.IsAny <BusinessTripDTO>())).Throws(new ValidationException("", ""));
            Mock <EmployeeService> emock      = new Mock <EmployeeService>();
            BusinessTripController controller = GetNewBusinessTripController(bmock.Object, emock.Object);

            ViewResult result = (await controller.Edit(new BusinessTripViewModel {
                Id = 2,
                Name = "02.09.2018_026"
            })) as ViewResult;

            BusinessTripViewModel model = result.ViewData.Model as BusinessTripViewModel;

            Assert.AreEqual(2, model.Id);
            Assert.AreEqual("02.09.2018_026", model.Name);
        }
        public async Task Edit_Get_ModelIsValid_RetrievesBusinessTripFromModel()
        {
            Mock <BusinessTripService> bmock = new Mock <BusinessTripService>();

            bmock.Setup(m => m.FindByIdAsync(It.IsAny <int?>())).ReturnsAsync((int?_id) => new BusinessTripDTO {
                Id   = _id.Value,
                Name = "02.09.2018_026"
            });
            Mock <EmployeeService> emock      = new Mock <EmployeeService>();
            BusinessTripController controller = GetNewBusinessTripController(bmock.Object, emock.Object);

            ViewResult result = (await controller.Edit(2)) as ViewResult;

            BusinessTripViewModel model = result.ViewData.Model as BusinessTripViewModel;

            Assert.AreEqual(2, model.Id);
            Assert.AreEqual("02.09.2018_026", model.Name);
        }