public async Task Add_Employee_Success_Return_CreatedAtAction_Test() { EmployeesController target = new EmployeesController(this.employeesRepository.Object, this.mapper.Object); IActionResult result = await target.Add(new EmployeeModel()); var createdAtActionObject = result as CreatedAtActionResult; Assert.IsNotNull(createdAtActionObject); this.employeesRepository.Verify(m => m.Add(It.IsAny <EmployeeModel>()), Times.Once); this.employeesRepository.Verify(m => m.Save(), Times.Once); }
public async Task Add_Employee_Save_Is_False_Throw_Exception_Test() { try { this.employeesRepository.Setup(m => m.Save()).Returns(Task.FromResult(false)); EmployeesController target = new EmployeesController(this.employeesRepository.Object, this.mapper.Object); IActionResult result = await target.Add(new EmployeeModel()); Assert.Fail("Should be an exception"); } catch (Exception ex) { Assert.AreEqual("Failed to add new employee", ex.Message); this.employeesRepository.Verify(m => m.Add(It.IsAny <EmployeeModel>()), Times.Once); this.employeesRepository.Verify(m => m.Save(), Times.Once); } }
public async Task ShouldAddEmployeeAndReturnOneWithId() { var userId = new Guid("{155E521A-3D64-46C7-939E-8A3FC29DD201}"); var employee = new EmployeeDTO(); var expected = new EmployeeDTO() { Id = Guid.NewGuid(), }; _mock.Mock <IUserManager>() .Setup(manager => manager.GetUserId(It.IsAny <ClaimsPrincipal>())) .Returns(userId); _mock.Mock <IEmployeesService>() .Setup(service => service.AddAsync(employee, userId)) .ReturnsAsync(expected); var actual = await _controller.Add(employee); ContentAssert.AreEqual(expected, actual); }
private void btnSave_Click(object sender, EventArgs e) { //if (txtFName.Text == "") //{ // msg += "Text field cannot be empty"; //} //else //{ //if (!ValidationHelper.LettersOnly(txtFName.Text)) //{ // MessageBox.Show("Employee first name must contain letters only!"); // return; // } ////} ////if (!ValidationHelper.LettersOnly(txtFName.Text)) ////{ //// MessageBox.Show("Employee first name must contain letters only!"); //// return; ////} //if (!ValidationHelper.LettersOnly(txtLName.Text)) //{ // MessageBox.Show("Employee last name must contain letters only!"); // return; //} //if (!ValidationHelper.IsPhoneValid(txtPhone.Text)) //{ // MessageBox.Show("Employee phone number must contain letters only!"); // return; //} //if (!ValidationHelper.IsEmail(txtEmail.Text)) //{ // MessageBox.Show("Employee email address is incorrect!"); // return; //} //validation if (!ValidationHelper.LettersOnly(txtFName.Text)) { MessageBox.Show("Employee first name must contain letters only!"); return; } if (!ValidationHelper.LettersOnly(txtLName.Text)) { MessageBox.Show("Employee last name must contain letters only!"); return; } if (!ValidationHelper.IsEmail(txtEmail.Text)) { MessageBox.Show("Employee email address is incorrect!"); return; } //if (!ValidationHelper.NumberOnly(txtDOB.Text)) //{ // MessageBox.Show("Employee date of birth must contain letters only!"); // return; //} //if (!ValidationHelper.IsValidDate(txtDOB.Text)) //{ // MessageBox.Show("Incorect date of birth!"); // return; //} //if (!ValidationHelper.NumberOnly(txtPhone.Text)) //{ // MessageBox.Show("Employee phone number must contain letters only!"); // return; //} //if (txtDOB.Text == "" || txtPhone.Text == "") //{ // MessageBox.Show("The text field can not be empty"); //} //read input Employees emp = new Employees(); emp.FirstName = txtFName.Text; emp.LastName = txtLName.Text; emp.Email = txtEmail.Text; emp.DOB = txtDOB.Text; emp.Phone = txtPhone.Text; //call controller - save into db EmployeesController controller = new EmployeesController(); ResultEnum result = controller.Add(emp); switch (result) { case ResultEnum.Success: MessageBox.Show("New employee added"); break; case ResultEnum.Fail: MessageBox.Show("Employee can not be added, please try again :-"); break; } // if (result == ResultEnum.Success) //{ // //show output ... // MessageBox.Show("New employee added"); //} //else //{ // MessageBox.Show("Employee can not be added, please try again :-"); //} }