Пример #1
0
        public void Edit()
        {
            // Arrange
            var controller = new EmployeesController(this.MockContext.Object);

            // Act
            var CompanyFindEdit = controller.Edit(2) as ViewResult;

            var mockedFindEditModel = CompanyFindEdit.Model as Employee;

            mockedFindEditModel.FullName = "Worker 2-Changed";

            controller.Edit(mockedFindEditModel);

            var result = controller.Index() as ViewResult;

            var mockedModels = result.Model as List <Employee>;

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(8, mockedModels.Count);
            Assert.AreEqual("Worker 1", mockedModels[0].FullName);
            Assert.AreEqual("Worker 2-Changed", mockedModels[1].FullName);
            Assert.AreEqual("Worker 3", mockedModels[2].FullName);
        }
Пример #2
0
        public void ReturnRedirectToRouteResult_WhenModelStateIsValid()
        {
            // Arrange
            var mockedMappService = new Mock <IMapService>();
            var employeeService   = new Mock <IEmployeeService>();
            EmployeesController emplController = new EmployeesController(mockedMappService.Object, employeeService.Object);
            var employee = new EmployeeViewModel()
            {
                Id         = 5,
                FirstName  = "Alex",
                MiddleName = "Parvanov",
                LastName   = "Petrov",
                PersonalId = "9010103040"
            };

            var employeeViewModel = new Employee()
            {
                Id         = 5,
                FirstName  = "Alex",
                MiddleName = "Parvanov",
                LastName   = "Petrov",
                PersonalId = "9010103040"
            };

            employeeService.Setup(x => x.UpdateById(5, employeeViewModel)).Verifiable();
            mockedMappService.Setup(x => x.Map <Employee>(employee)).Returns(employeeViewModel);
            // Act
            emplController.Edit(employee);

            // Assert
            Assert.IsInstanceOf <RedirectToRouteResult>(emplController.Edit(employee));
        }
Пример #3
0
        public void TestGetEditReturnsView()
        {
            mockUnitOfWork.Setup(m => m.EmployeeRepository.GetByID(0)).Returns(employees[0]);
            var result = controller.Edit(0) as ViewResult;

            Assert.IsNotNull(result);
            Assert.AreEqual("Edit", result.ViewName);
        }
Пример #4
0
        public async Task EditGetAction_UpdateModelFromService()
        {
            // Arrange

            // Act
            var result = await validController.Edit(entity.Id);

            // Assert
            mock.Verify(a => a.GetAsync(entity.Id, true));
        }
Пример #5
0
        public async Task EditWhenEmailNull()
        {
            // ARRANGE
            string email = null;

            // ACT
            var resultTask = _controller.Edit(email);

            // Type cast
            var result = await resultTask as HttpStatusCodeResult;

            // ASSERT
            Assert.AreEqual((int)HttpStatusCode.BadRequest, result.StatusCode);
        }
Пример #6
0
        public void ReturnViewResult_WhenModelStateIsNotValid()
        {
            // Arrange
            var mockedMappService = new Mock <IMapService>();
            var employeeService   = new Mock <IEmployeeService>();
            EmployeesController emplController = new EmployeesController(mockedMappService.Object, employeeService.Object);
            EmployeeViewModel   employee       = null;

            emplController.ModelState.AddModelError("invalid", "invalid");
            // Act
            emplController.Edit(employee);

            // Assert
            Assert.IsInstanceOf <ViewResult>(emplController.Edit(employee));
        }
        public async Task Edit_WhenCalled_ReturnEmployee()
        {
            // Arrange
            repository.Setup(r => r.GetEmployeeAsync(1)).ReturnsAsync(employee);

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

            var model = result.Model as EmployeeEditViewModel;

            // Assert
            Assert.That(result, Is.Not.Null);
            Assert.That(model.Id, Is.EqualTo(1));
            Assert.That(model.Position, Is.EqualTo(Position.Manager));
        }
Пример #8
0
        public void Edit_WhenCalled_ReturnView()
        {
            var result = controller.Edit();

            Assert.That(result.ViewName, Is.EqualTo(string.Empty));
            Assert.That(result, Is.TypeOf(typeof(ViewResult)));
        }
Пример #9
0
        public void CannotEditNonexistentEmployee()
        {
            var employeesController = new EmployeesController(_mock.Object);
            var result = employeesController.Edit(6) as ViewResult;

            Assert.IsNull(result);
        }
        public void TestEditView()
        {
            var employeesCorntorller = new EmployeesController();
            var result = employeesCorntorller.Edit(1) as ViewResult;

            Assert.AreEqual("Edit", result.ViewName);
        }
        public async void Edit_HttpPost_DbUpdateConcurrencyException_EmployeeExist_ReturnsThrow()
        {
            // Arange
            var id       = 1;
            var employee = new Employee {
                EmployeeId = 1
            };

            var employeeRepositroy = new Mock <IEmpoyeeRepository>();

            employeeRepositroy.Setup(mock =>
                                     mock.UpdateEmployee(employee)).Throws(
                new DbUpdateConcurrencyException(string.Empty, new List <IUpdateEntry> {
                Mock.Of <IUpdateEntry>()
            }));

            employeeRepositroy.Setup(mock => mock.EmployeeExists(id)).Returns(true);

            var employeeController = new EmployeesController(employeeRepositroy.Object);

            // Act & Assert
            await Assert.ThrowsAnyAsync <Exception>(() => employeeController.Edit(id, employee));

            employeeRepositroy.Verify(mock => mock.UpdateEmployee(employee), Times.Once());
            employeeRepositroy.Verify(mock => mock.EmployeeExists(id), Times.Once());

            employeeController.Dispose();
        }
        public async void Edit_HttpPost_DbUpdateConcurrencyException_EmployeeNotExist_ReturnsNotFound()
        {
            // Arange
            var id       = 1;
            var employee = new Employee {
                EmployeeId = 1
            };

            var employeeRepositroy = new Mock <IEmpoyeeRepository>();

            employeeRepositroy.Setup(mock =>
                                     mock.UpdateEmployee(employee)).Throws(
                new DbUpdateConcurrencyException(string.Empty, new List <IUpdateEntry> {
                Mock.Of <IUpdateEntry>()
            }));

            employeeRepositroy.Setup(mock => mock.EmployeeExists(id)).Returns(false);

            var employeeController = new EmployeesController(employeeRepositroy.Object);

            // Act
            var result = await employeeController.Edit(id, employee) as NotFoundResult;

            // Assert
            employeeRepositroy.Verify(mock => mock.UpdateEmployee(employee), Times.Once());
            employeeRepositroy.Verify(mock => mock.EmployeeExists(id), Times.Once());

            Assert.NotNull(result);

            employeeController.Dispose();
        }
Пример #13
0
        public void CanEditEmployee()
        {
            var employeesController = new EmployeesController(_mock.Object);
            var result = employeesController.Edit(1) as ViewResult;

            var e1 = result.ViewData.Model as Employee;

            result = employeesController.Edit(2) as ViewResult;
            var e2 = result.ViewData.Model as Employee;

            result = employeesController.Edit(3) as ViewResult;
            var e3 = result.ViewData.Model as Employee;

            Assert.AreEqual(1, e1.EmployeeId);
            Assert.AreEqual(2, e2.EmployeeId);
            Assert.AreEqual(3, e3.EmployeeId);
        }
Пример #14
0
        public void ReturnHttpNotFoundResult_WhenEmployeeIsNull()
        {
            // Arrange
            var mockedMappService = new Mock <IMapService>();
            var employeeService   = new Mock <IEmployeeService>();
            EmployeesController emplController = new EmployeesController(mockedMappService.Object, employeeService.Object);
            var      employeeId = 5;
            Employee employee   = null;

            employeeService.Setup(x => x.GetById(employeeId)).Returns(employee);

            // Act
            emplController.Edit(employeeId);

            // Assert
            Assert.IsInstanceOf <HttpNotFoundResult>(emplController.Edit(employeeId));
        }
Пример #15
0
        public void ReturnViewResult_WhenIdIsCorrect()
        {
            // Arrange
            var mockedMappService = new Mock <IMapService>();
            var employeeService   = new Mock <IEmployeeService>();
            EmployeesController emplController = new EmployeesController(mockedMappService.Object, employeeService.Object);
            var employeeId = 5;

            employeeService.Setup(x => x.GetById(employeeId)).Returns(new Employee()
            {
                Id = employeeId
            });
            // Act
            emplController.Edit(employeeId);

            // Assert
            Assert.IsInstanceOf <ViewResult>(emplController.Edit(employeeId));
        }
Пример #16
0
        public async Task EditPostAction_ModelError_ReturnsSameModel()
        {
            // Arrange

            // Act
            var result = await notValidController.Edit(entity.Id, entity) as ViewResult;

            // Assert
            Assert.Equal(entity, result.Model);
        }
        public async System.Threading.Tasks.Task EmployeesEditReturnsViewAsync()
        {
            //Arrange
            Employees employeeTest = new Employees()
            {
                FirstName = "test", LastName = "test"
            };
            await _EmployeesControllerUnderTest.Create(employeeTest);

            //Act
            var result = _EmployeesControllerUnderTest.Edit(employeeTest.EmployeeID);

            //Assert
            Assert.IsNotNull(result);



            DeleteTestFromDb();
        }
        public async void Edit_IdIsNull_ReturnsNotFound()
        {
            var employeeRepositroy = new Mock <IEmpoyeeRepository>();
            var employeeController = new EmployeesController(employeeRepositroy.Object);

            var result = await employeeController.Edit(null) as NotFoundResult;

            Assert.NotNull(result);

            employeeController.Dispose();
        }
Пример #19
0
        public void CanSaveValidEmployeeChanges()
        {
            var employeesController = new EmployeesController(_mock.Object);
            var employee            = new Employee {
                Name = "Test"
            };
            var result = employeesController.Edit(employee) as RedirectToRouteResult;

            _mock.Verify(m => m.SaveEmployee(employee));
            Assert.AreEqual("Index", result.RouteValues["action"]);
        }
        public void EditView()
        {
            // Arrange
            EmployeesController controller = new EmployeesController();

            // Act
            ViewResult result = controller.Edit(1) as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }
        public void Edit_Negative_Test_Post_Mock_Test()
        {
            //Arrange
            _employee    = TestDataHelper.CreateEmployeeWithValidData();
            _employee.Id = 100;
            _fakeEmployeeDbSet.Add(_employee);

            _employee    = TestDataHelper.CreateEmployeeWithValidData();
            _employee.Id = 200;
            _fakeEmployeeDbSet.Add(_employee);

            _mockDbContext.Setup(db => db.EmployeeRepository).Returns(_fakeEmployeeDbSet);

            //Act
            _employeeController.FireValidationForModel(_employee);
            _employeeController.Edit(_employee);

            //Assert
            Assert.IsTrue(EmployeeAddressMustBeUnique.IsErrorAvalilableIn(_employeeController, _employee));
        }
Пример #22
0
        public void CannotSaveInvalidChanges()
        {
            var employeesController = new EmployeesController(_mock.Object);
            var employee            = new Employee {
                Name = "Test"
            };

            employeesController.ModelState.AddModelError("error", "error");
            employeesController.Edit(employee);
            _mock.Verify(x => x.SaveEmployee(employee), Times.Never());
        }
Пример #23
0
        public void PostTest()
        {
            var expectedEmployee = new Employee();
            var mock             = new Mock <IManager <Employee> >();
            var controller       = new EmployeesController(mock.Object);

            var result = controller.Edit(expectedEmployee) as RedirectToRouteResult;

            mock.Verify(m => m.Update(It.Is <Employee>(x => x == expectedEmployee)), Times.Once);
            Assert.IsNotNull(result);
            Assert.AreEqual("Index", result.RouteValues["action"]);
        }
Пример #24
0
        public void testEmployeesControllerIndexIdAndCompany()
        {
            //Arrange
            DbContextOptions <ProjectContext> context = new DbContextOptions <ProjectContext>();
            ProjectContext      projectContext        = new ProjectContext(context);
            EmployeesController controller            = new EmployeesController(projectContext);

            //Act
            var result = controller.Edit(1, null);

            //Assert
            Assert.NotNull(result);
        }
        public async void Edit_GetEmployeeById_ResusltIsNull_ReturnsNotFound()
        {
            var employeeRepositroy = new Mock <IEmpoyeeRepository>();

            employeeRepositroy.Setup(mock => mock.GetEmployeeById(1)).Returns(Task.FromResult <Employee>(null));
            var employeeController = new EmployeesController(employeeRepositroy.Object);

            var result = await employeeController.Edit(1) as NotFoundResult;

            employeeRepositroy.Verify(mock => mock.GetEmployeeById(1), Times.Once());
            Assert.NotNull(result);

            employeeController.Dispose();
        }
Пример #26
0
        public async void ShouldReturnNotFoundWhenUpdatingAnIdNotInDB()
        {
            //Arrange
            var dbContext = serviceProvider.GetRequiredService <EmployeeContext>();

            CreateTestData(dbContext);
            var controller = new EmployeesController(dbContext);

            //Act
            var editResult = await controller.Edit(100);

            //Assert
            Assert.IsType <NotFoundResult>(editResult);
        }
        public async void Edit_HttpPost_IdNotEqualToEmployeeId_ReturnsNotFound()
        {
            var employeeRepositroy = new Mock <IEmpoyeeRepository>();
            var employeeController = new EmployeesController(employeeRepositroy.Object);
            var id       = 1;
            var employee = new Employee {
                EmployeeId = 2
            };

            var result = await employeeController.Edit(id, employee) as NotFoundResult;

            Assert.NotNull(result);

            employeeController.Dispose();
        }
        public void EditTest()
        {
            var controller   = new EmployeesController();
            var result       = controller.Index() as ViewResult;
            var employees    = (ICollection <Employee>)result.ViewData.Model;
            var editEmployee = employees.Last();

            editEmployee.FirstName = "NewEmployeeFName";
            editEmployee.LastName  = "NewEmployeeLName";
            controller.Edit(editEmployee);

            var result2    = controller.Index() as ViewResult;
            var employees2 = (ICollection <Employee>)result2.ViewData.Model;

            Assert.AreEqual("NewEmployeeFName", employees2.Last().FirstName);
        }
        public async void Edit_GetEmployeeById_EmployeeFound_ReturnsViewEdit_ModelEmployee()
        {
            // arange
            var employeeRepositroy = new Mock <IEmpoyeeRepository>();

            employeeRepositroy.Setup(mock => mock.GetEmployeeById(1)).ReturnsAsync(new Employee());
            var employeeController = new EmployeesController(employeeRepositroy.Object);

            // act
            var result = await employeeController.Edit(1) as ViewResult;

            // assert
            Assert.Equal("Edit", result.ViewName);
            employeeRepositroy.Verify(mock => mock.GetEmployeeById(1), Times.Once());
            Assert.NotNull(result);

            employeeController.Dispose();
        }
        public async void Edit_HttpPost_UpdateEmployee_RedirectToIndexAction()
        {
            var id       = 1;
            var employee = new Employee {
                EmployeeId = 1
            };

            var employeeRepositroy = new Mock <IEmpoyeeRepository>();

            employeeRepositroy.Setup(mock => mock.UpdateEmployee(employee));
            var employeeController = new EmployeesController(employeeRepositroy.Object);

            var result = await employeeController.Edit(id, employee);

            employeeRepositroy.Verify(mock => mock.UpdateEmployee(employee), Times.Once());
            var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result);

            Assert.Equal("Index", redirectToActionResult.ActionName);

            employeeController.Dispose();
        }