public void CreatePost_CannotCreate_InvalidVisaRegistrationDate() { // Arrange - create the controller VisaRegistrationDateController target = new VisaRegistrationDateController(mock.Object, messengerMock.Object); VisaRegistrationDate visaRegDate = new VisaRegistrationDate(); // Act - call the action method target.ModelState.AddModelError("error", "error"); ViewResult result = target.Create(visaRegDate) as ViewResult; // Assert - check the result mock.Verify(m => m.SaveVisaRegistrationDate(It.IsAny <VisaRegistrationDate>(), It.IsAny <int>()), Times.Never); Assert.IsInstanceOf(typeof(RegistrationDateViewModel), result.ViewData.Model); Assert.IsInstanceOf(typeof(ViewResult), result); }
public void CreateGet_VisaRegistrationDateOf_NotExistingEmployee() { // Arrange - create the controller VisaRegistrationDateController target = new VisaRegistrationDateController(mock.Object, messengerMock.Object); // Act - call the action method var result = (HttpNotFoundResult)target.Create(1500); VisaRegistrationDate visaRegDate = mock.Object.VisaRegistrationDates.Where(m => m.EmployeeID == 1500).FirstOrDefault(); Employee employee = mock.Object.Employees.Where(m => m.EmployeeID == 1500).FirstOrDefault(); // Assert - check the result Assert.IsNull(visaRegDate); Assert.IsNull(employee); Assert.AreEqual(404, result.StatusCode); Assert.IsInstanceOf(typeof(HttpNotFoundResult), result); }
public void CreateGet_VisaRegistrationDateOf_ExistingEmployeeSearchStringEmpty() { // Arrange - create the controller VisaRegistrationDateController target = new VisaRegistrationDateController(mock.Object, messengerMock.Object); MvcApplication.JSDatePattern = "dd.mm.yyyy"; // Act - call the action method var result = target.Create(5) as ViewResult; VisaRegistrationDate visaRegDate = (VisaRegistrationDate)result.ViewData.Model; // Assert - check the result Assert.AreEqual("", result.ViewName); Assert.AreEqual("", result.ViewBag.SearchString); Assert.AreEqual("dd.mm.yyyy", result.ViewBag.JSDatePattern); Assert.IsInstanceOf(typeof(ViewResult), result); Assert.AreEqual("Daolson Ivan (daol) from RAAA4", result.ViewBag.EmployeeInformation); Assert.IsNull(visaRegDate); }
public void CreatePost_ValidModelConcurrency_ErrorReturned() { //Arrange VisaRegistrationDateController controller = new VisaRegistrationDateController(mock.Object, messengerMock.Object); string modelError = "The record you attempted to edit " + "was modified by another user after you got the original value. The " + "edit operation was canceled."; VisaRegistrationDate visaRegDate = mock.Object.VisaRegistrationDates.Where(p => p.EmployeeID == 1).FirstOrDefault(); mock.Setup(m => m.SaveVisaRegistrationDate(visaRegDate, visaRegDate.EmployeeID)).Throws(new InvalidOperationException()); //Act var result = controller.Create(visaRegDate); string data = (string)(new Microsoft.VisualStudio.TestTools.UnitTesting.PrivateObject(((JsonResult)result).Data, "error")).Target; //Assert mock.Verify(d => d.SaveVisaRegistrationDate(visaRegDate, visaRegDate.EmployeeID), Times.Once()); Assert.AreEqual(typeof(JsonResult), result.GetType()); Assert.AreEqual(modelError, data); }
public void CreatePost_CanCreate_ValidVisaRegistrationDate() { // Arrange - create the controller VisaRegistrationDateController target = new VisaRegistrationDateController(mock.Object, messengerMock.Object); VisaRegistrationDate visaRegDate = new VisaRegistrationDate { EmployeeID = 5, RegistrationDate = new DateTime(2013, 04, 01), VisaType = "D10", City = "Kyiv", RegistrationTime = "09:00", RegistrationNumber = "0001" }; MvcApplication.JSDatePattern = "dd.mm.yyyy"; target.ControllerContext = controllerContext.Object; // Act - call the action method var result = target.Create(visaRegDate, "as"); // Assert - check the result mock.Verify(m => m.SaveVisaRegistrationDate(visaRegDate, 5), Times.Once); Assert.AreEqual("as", ((ViewResult)result).ViewBag.SearchString); Assert.AreEqual("dd.mm.yyyy", ((ViewResult)result).ViewBag.JSDatePattern); Assert.AreEqual("TableViewVisasAndPermitsBTM", ((ViewResult)result).ViewName); Assert.IsInstanceOf(typeof(List <Employee>), ((ViewResult)result).Model); }