Beispiel #1
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 Employee()
            {
                Id         = 5,
                FirstName  = "Elena",
                MiddleName = "Ivanova",
                LastName   = "Petrova",
                PersonalId = "9010103040"
            };

            var employeeViewModel = new EmployeeViewModel();

            employeeService.Setup(x => x.Create(employee)).Verifiable();
            mockedMappService.Setup(x => x.Map <EmployeeViewModel>(employee)).Returns(employeeViewModel);
            // Act
            emplController.Create(employeeViewModel);

            // Assert
            Assert.IsInstanceOf <RedirectToRouteResult>(emplController.Create(employeeViewModel));
        }
Beispiel #2
0
        public void Create()
        {
            // Arrange
            var controller = new EmployeesController(this.MockContext.Object);

            // Act
            controller.Create();

            var employee = new Employee()
            {
                CompanyId    = 1,
                FullName     = "Worker 9",
                Salary       = 2500.5m,
                StartDate    = DateTime.Now,
                VacationDays = 15.0m,
                Experiance   = ExperianceLevel.B
            };

            controller.Create(employee);
            var result       = controller.Index() as ViewResult;
            var mockedModels = result.Model as List <Employee>;

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(9, mockedModels.Count);
            Assert.AreEqual("Worker 9", mockedModels[8].FullName);
        }
Beispiel #3
0
        public async Task CreatePostAction_AddModelToService()
        {
            // Arrange

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

            // Assert
            mock.Verify(a => a.AddAsync(entity));
        }
Beispiel #4
0
        public void ReturnViewResult()
        {
            // Arrange
            var mockedMappService = new Mock <IMapService>();
            var employeeService   = new Mock <IEmployeeService>();
            EmployeesController emplController = new EmployeesController(mockedMappService.Object, employeeService.Object);

            // Act
            emplController.Create();

            // Assert
            Assert.IsInstanceOf <ViewResult>(emplController.Create());
        }
Beispiel #5
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   employeeViewModel = null;

            emplController.ModelState.AddModelError("invalid", "invalid");
            // Act
            emplController.Create(employeeViewModel);

            // Assert
            Assert.IsInstanceOf <ViewResult>(emplController.Create(employeeViewModel));
        }
        public void TestCreateView()
        {
            var employeesCorntorller = new EmployeesController();
            var result = employeesCorntorller.Create() as ViewResult;

            Assert.AreEqual("Create", result.ViewName);
        }
        public void The_Add_Employee_Action_Returns_RedirectToRouteResult_When_The_Employee_Model_Is_Valid()
        {
            // Arrange
            const string expectedRouteName = "EmployeeCreated";
            var          employee          = new Employee()
            {
                EmployeeID = 1,
                Name       = "Aimen",
                Address    = "Address",
                Age        = 1,
                Employment = 1,
                Active     = 1
            };

            var employeesController = new EmployeesController(_mockER.Object, _mockUW.Object);
            var routeData           = new RouteData();

            _FormKeys.Add("Create", "Create");
            employeesController.ControllerContext =
                new ControllerContext(_mockHttpContext.Object, routeData, employeesController);
            employeesController.ModelState.Clear();
            // Act
            var result = employeesController.Create(employee) as RedirectToRouteResult;

            // Assert
            Assert.AreEqual(4, _data.Count());
            Assert.IsNotNull(result, "Should have returned a RedirectToRouteResult");
            // Assert.AreEqual(expectedRouteName, result.RouteName, "Route name should have been {0}", expectedRouteName);
        }
        public async Task Create_WhenCalled_CreateEmployeeInDbAndRedirectToIndex()
        {
            // Arrange
            var model = new EmployeeCreateViewModel
            {
                FirstName = "Ocean",
                LastName  = "Breeze",
                Phone     = "111 2222 3333",
                Email     = "*****@*****.**",
                Position  = Position.Waiter,
                JobType   = JobType.Parttime,
                Postcode  = "2000",
                StateCode = "NSW",
                Unit      = "32",
                Street    = "Market",
                Town      = "Sydney"
            };

            // Act
            var result = await controller.Create(model) as RedirectToActionResult;

            // Assert
            repository.Verify(r => r.AddAsync(It.IsAny <Employee>()), Times.Once);
            unitOfWork.Verify(u => u.CompleteAsync(), Times.Once);
            Assert.That(result.ActionName, Is.EqualTo("index").IgnoreCase);
        }
        public void CreateViewTest()
        {
            var mock       = new Mock <IManager <Employee> >();
            var controller = new EmployeesController(mock.Object);

            var result = controller.Create() as ViewResult;

            Assert.IsNotNull(result);
        }
Beispiel #10
0
        private async Task CreateEmployeesAsync()
        {
            var c = new EmployeesController(_context);

            foreach (var employee in employees)
            {
                var result = await c.Create(employee);
            }
        }
        public void CreateView()
        {
            // Arrange
            EmployeesController controller = new EmployeesController();

            // Act
            ViewResult result = controller.Create() as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }
Beispiel #12
0
        public void CanCreateANewEmployee()
        {
            var employeesController = new EmployeesController(_mock.Object);
            var employee            = new Employee {
                Name = "TestName"
            };
            var viewResult = employeesController.Create(employee) as RedirectToRouteResult;

            Assert.AreEqual("Index", viewResult.RouteValues["action"]);
            _mock.Verify(x => x.SaveEmployee(employee), Times.Once());
        }
Beispiel #13
0
        public async Task CreatePostAction_ModelError_ReturnsSameModel()
        {
            // Arrange


            // Act
            var result = await notValidController.Create(entity) as ViewResult;

            // Assert
            Assert.Equal(entity, result.Model);
        }
        public void Create_HttpGet_ReturnsCreateView()
        {
            var employeeRepositroy = new Mock <IEmpoyeeRepository>();
            var employeeController = new EmployeesController(employeeRepositroy.Object);

            var result = employeeController.Create() as ViewResult;

            Assert.Equal("Create", result.ViewName);

            employeeController.Dispose();
        }
        public void CreateTest()
        {
            var expectedEmployee = new Employee();
            var mock             = new Mock <IManager <Employee> >();
            var controller       = new EmployeesController(mock.Object);

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

            mock.Verify(m => m.Insert(It.Is <Employee>(x => x == expectedEmployee)), Times.Once);
            Assert.IsNotNull(result);
            Assert.AreEqual("Index", result.RouteValues["action"]);
        }
        public void CreateReturnsView()
        {
            //Arrange

            //Act
            var result = _EmployeesControllerUnderTest.Create() as ViewResult;

            //Assert
            Assert.IsNotNull(result);
        }
Beispiel #17
0
        public void CannotCreateAnInvalidEmployee()
        {
            var employeesController = new EmployeesController(_mock.Object);

            employeesController.ModelState.AddModelError("error", "error");
            var employee   = new Employee();
            var viewResult = employeesController.Create(employee) as ViewResult;
            var model      = viewResult.Model;

            Assert.AreEqual(model, employee);
            Assert.AreEqual("Create", viewResult.ViewName);
            _mock.Verify(x => x.SaveEmployee(employee), Times.Never());
        }
Beispiel #18
0
        public void testEmployeesControllerCreate()
        {
            //Arrange
            DbContextOptions <ProjectContext> context = new DbContextOptions <ProjectContext>();
            ProjectContext      projectContext        = new ProjectContext(context);
            EmployeesController controller            = new EmployeesController(projectContext);

            //Act
            var result = controller.Create();

            //Assert
            Assert.NotNull(result);
        }
Beispiel #19
0
        public async Task ShouldReturnViewFromInvalidModelState()
        {
            //Arrange
            var dbContext = serviceProvider.GetRequiredService <EmployeeContext>();

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

            controller.ModelState.AddModelError("error", "error");

            //Act
            var result = await controller.Create(null);

            //Assert
            Assert.IsType <ViewResult>(result);
        }
        private async void BtnSaveChanges_Click(object sender, EventArgs e)
        {
            EmployeesController eCtrl = new EmployeesController();
            DashboardGUI        dGUI  = DashboardGUI.Instance;
            VenuesController    vCtrl = new VenuesController();

            if (TextBoxesHasValues())
            {
                if (create)
                {
                    //Venue currVenue = (Venue)cbVenueID.SelectedItem;
                    //Debug.WriteLine("VenueId from tableGUI: " + currVenue.Id);
                    string Password = HashingHelper.GenerateHash(tbPassword.Text);
                    string Salt     = HashingHelper.RandomString(20);
                    try {
                        Employee newEmployee = new Employee {
                            Name = tbName.Text, Phone = tbPhone.Text, Email = tbEmail.Text, Password = Password, Salt = Salt, EmployeeNo = await eCtrl.GetNewEmployeeNo(), Title = tbEmployeeTitle.Text
                        };
                        Venue newVenue = (Venue)cbVenueID.SelectedItem;
                        Debug.WriteLine("newEmployee: " + newEmployee.Id + " " + newEmployee.Name);
                        Debug.WriteLine("newVenue: " + newVenue.Id + " " + newVenue.Name);

                        await eCtrl.Create(newEmployee, newVenue);

                        this.Visible = false;
                        dGUI.AddEmployeeStats();
                    }
                    catch (Exception) {
                        MessageBox.Show("No connection to service");
                    }
                }
                else if (!create)
                {
                    MessageBox.Show("Method not implemented");
                    this.Visible = false;

                    /*int id = editedTable.Id;
                     * Table updatedTable = await tCtrl.Get(editedTable.Id);
                     * await tCtrl.Update(updatedTable.Id, updatedTable);
                     * this.Visible = false;*/
                }
            }
            else
            {
                MessageBox.Show("Error...One or more fields are empty!");;
            }
        }
        public void CreateTest()
        {
            var newEmployee = new Employee();

            newEmployee.FirstName      = "TestEmployeeFName";
            newEmployee.LastName       = "TestEmployeeLName";
            newEmployee.EmploymentDate = new DateTime(2017, 11, 11);
            newEmployee.DepartmentId   = 1;

            var controller = new EmployeesController();

            controller.Create(newEmployee);
            var result    = controller.Index() as ViewResult;
            var employees = (ICollection <Employee>)result.ViewData.Model;

            Assert.AreEqual("TestEmployeeFName", employees.Last().FirstName);
        }
        public async void Create_HttpPost_AddEmployee_RedirectToIndexAction()
        {
            var employeeRepositroy = new Mock <IEmpoyeeRepository>();
            var employee           = new Employee();

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

            var result = await employeeController.Create(employee);

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

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

            employeeController.Dispose();
        }
Beispiel #23
0
        public void CreateSQL()
        {
            //Arrange.
            var EmployeeRepository  = new EmployeeRepository();
            var EmployeesController = new EmployeesController(EmployeeRepository);
            var expected            = new Employees()
            {
                FullName = "Чусовлянкин Алексей Александрович",
                Position = "Директор",
                Salary   = 100000,
                Status   = "активен"
            };

            //Act.
            EmployeesController.Create(expected);
            var id    = expected.Id;
            int count = 0;

            using (SqlConnection cn = new SqlConnection {
                ConnectionString = ConfigurationManager.ConnectionStrings["EmployeesContextTest"].ConnectionString
            })
            {
                using (SqlCommand cmd = new SqlCommand {
                    Connection = cn
                })
                {
                    cmd.CommandText = $"select Id, FullName, Position, Status, Salary from [dbo].[list_employees] where Id = {id}";
                    cn.Open();
                    using (SqlDataReader MyReader = cmd.ExecuteReader())
                    {
                        while (MyReader.Read())
                        {
                            string[] row = new string[MyReader.FieldCount];
                            for (int i = 0; i < MyReader.FieldCount; i++)
                            {
                                row[i] = MyReader[i].ToString().Trim();
                                count  = 1;
                            }
                        }
                    }
                }
            }

            //Assert.
            Assert.AreEqual(count, 1);
        }
        public void DeleteRecord()
        {
            // Arrange
            EmployeesController controller = new EmployeesController();

            // Act
            ViewResult result             = controller.Index() as ViewResult;
            int        iRecordCountBefore = (result.Model as List <Employee>).Count();

            Employee employee = new Employee();

            employee.DepartmentId = 1;
            employee.DeskLocation = "X999";
            employee.FirstName    = "Testy";
            employee.LastName     = "McTesterson";
            employee.HomeAddress1 = "123 Banana Boulevard";
            employee.HomeAddress2 = null;
            employee.HomeCity     = "Las Vegas";
            employee.HomePhone    = "123-456-7890";
            employee.HomeState    = "NV";
            employee.HomeZip      = "88888";
            employee.WorkPhone    = "123-456-0987";
            result = controller.Create(employee) as ViewResult; // Create the DB record.
            result = controller.Index() as ViewResult;          // Read a list back.

            List <Employee> employees         = result.Model as List <Employee>;
            int             iRecordCountAfter = employees.Count();
            bool            addedRecord       = (iRecordCountBefore + 1 == iRecordCountAfter);
            int             employeeId        = employees.First(x => (x.LastName == "McTesterson")).EmployeeId;

            // Clean up and delete confirmation.
            bool deleteWorked = true; // Assuming true as default for this test.

            try
            {
                result = controller.DeleteConfirmed(employeeId) as ViewResult;
            }
            catch
            {
                deleteWorked = false;
            }

            // Assert
            Assert.IsTrue(deleteWorked);
        }
Beispiel #25
0
        public void Create()
        {
            //Arrange.
            var expected = new Employees()
            {
                FullName = "Чусовлянкин Алексей Александрович",
                Position = "Директор",
                Salary   = 100000,
                Status   = "активен"
            };

            //Act.
            EmployeesController.Create(expected);
            var actual = EmployeeRepository.GetEmployeeById(expected.Id);

            //Assert.
            Assert.AreEqual(actual, expected);
        }
        public void TestCreate_ShouldAddAnEmployeeToTheDatabaseAndGoBackToIndexView()
        {
            //AAA
            //Arrange
            EmployeesController controllerUnderTest = new EmployeesController(mockUnitOfWork.Object, mockBonusProjector.Object);
            string employeeTestName  = "Test Employee " + DateTime.Now.ToLongTimeString();
            var    employeeToBeAdded = new Employee {
                Name = employeeTestName, Salary = 100000, HireDate = DateTime.Now, PerformanceStarLevel = 1
            };
            //Act
            var returnedResult = controllerUnderTest.Create(employeeToBeAdded).Result;

            //Assert
            Assert.AreEqual(returnedResult.GetType(), typeof(RedirectToRouteResult));
            var returnedView = returnedResult as RedirectToRouteResult;

            Assert.AreEqual(returnedView.RouteValues["Action"], "Index");
        }
        public async void Create_HttpPost_ModelStateNotValid_ReturnsCreateView()
        {
            // arange
            var employeeRepositroy = new Mock <IEmpoyeeRepository>();
            var employeeController = new EmployeesController(employeeRepositroy.Object);

            employeeController.ModelState.AddModelError("test", "test");
            var employee = new Employee();

            // act
            var result = await employeeController.Create(employee) as ViewResult;

            // assert
            Assert.False(employeeController.ModelState.IsValid);
            Assert.Equal("Create", result.ViewName);
            Assert.Same(employee, result.Model as Employee);

            employeeController.Dispose();
        }
Beispiel #28
0
        public async Task ShouldReturnAListWithCreatedEmployee()
        {
            //Arrange
            var dbContext = serviceProvider.GetRequiredService <EmployeeContext>();

            CreateTestData(dbContext);
            var      controller = new EmployeesController(dbContext);
            Employee e          = new Employee {
                LastName = "Adams", FirstName = "JohnQuincy"
            };

            //Act
            var result = await controller.Create(e);

            //Assert
            var actionResult = Assert.IsType <RedirectToActionResult>(result);
            var view         = Assert.IsType <ViewResult>(await controller.Index());
            var model        = Assert.IsAssignableFrom <IEnumerable <Employee> >(view.ViewData.Model);

            Assert.Equal(6, model.Count());
        }
        public void EditRecord()
        {
            // Arrange
            EmployeesController controller = new EmployeesController();

            // Act
            ViewResult result             = controller.Index() as ViewResult;
            int        iRecordCountBefore = (result.Model as List <Employee>).Count();

            Employee employee = new Employee();

            employee.DepartmentId = 1;
            employee.DeskLocation = "X999";
            employee.FirstName    = "Testy";
            employee.LastName     = "McTesterson";
            employee.HomeAddress1 = "123 Banana Boulevard";
            employee.HomeAddress2 = null;
            employee.HomeCity     = "Las Vegas";
            employee.HomePhone    = "123-456-7890";
            employee.HomeState    = "NV";
            employee.HomeZip      = "88888";
            employee.WorkPhone    = "123-456-0987";
            result = controller.Create(employee) as ViewResult; // Create the DB record.

            employee.FirstName = "Testarino";
            result             = controller.Edit(employee) as ViewResult; // Edit the DB record.

            result = controller.Index() as ViewResult;                    // Read a list back.
            List <Employee> employees = result.Model as List <Employee>;

            int employeeId = employees.First(x => (x.FirstName == "Testarino")).EmployeeId; // Find the record we added.

            if (employeeId > -1)
            {
                controller.DeleteConfirmed(employeeId); // Clean up.
            }
            // Assert
            Assert.IsTrue((employeeId > -1));
        }
        public void The_Add_Employee_Action_Returns_ViewResult_When_The_Employee_Model_Is_Invalid()
        {
            // Arrange
            const string expectedRouteName = "EmployeeCreated";
            var          employee          = new Employee()
            {
                EmployeeID = 1,
                Name       = "Aimen",
                Address    = "Address",
                Age        = 1,
                Employment = 1,
                Active     = 1
            };

            var employeesController = new EmployeesController(_mockER.Object, _mockUW.Object);

            employeesController.ModelState.AddModelError("A Error", "Message");
            // Act
            var result = employeesController.Create(employee) as RedirectToRouteResult;

            // Assert
            Assert.AreEqual(3, _data.Count());
            Assert.IsNull(result, "Should have returned a RedirectToRouteResult");
        }