public void Create_Task_Model_isInvalid() { #region Arrange TaskListModel mockTaskList = new TaskListModel { ID = 1, TaskCompleted = false, TaskDescription = null }; this.mockContext.SetupGet(x => x.Request).Returns(this.mockRequest.Object); this.mockContext.SetupGet(x => x.Response).Returns(this.mockResponse.Object); mockContext.Setup(x => x.Session["InMemoryTaskList"]).Returns(null); var mockControllerContext = new RequestContext(this.mockContext.Object, new RouteData()); TaskListController controller = new TaskListController(); controller.tasklist = mockTasklist.Object; controller.ControllerContext = new ControllerContext(mockControllerContext, controller); controller.ViewData.ModelState.AddModelError("TaskDescription", "Please specify a task description before submitting again"); #endregion #region Act var result = (ViewResult)controller.Create(mockTaskList); var viewModel = controller.ViewData.Model as TaskListModel; #endregion #region Assert Assert.IsFalse(result.ViewData.ModelState.IsValid); Assert.IsNull(controller.HttpContext.Session["InMemoryTaskList"]); #endregion }
public void Create_2nd_Task_Success() { #region Arrange List <TaskDetailsModel> mockTaskList = new List <TaskDetailsModel>(); TaskDetailsModel mock1stTaskDetails = new TaskDetailsModel { ID = 1, TaskCompleted = false, TaskDescription = HttpUtility.HtmlEncode("Mocked Task") }; TaskDetailsModel mock2ndTaskDetails = new TaskDetailsModel { ID = 2, TaskCompleted = false, TaskDescription = HttpUtility.HtmlEncode("2nd Mocked Task") }; TaskListModel mockTaskListModel = new TaskListModel(); mockTaskList.Add(mock1stTaskDetails); mockTaskListModel.ID = mock2ndTaskDetails.ID; mockTaskListModel.TaskCompleted = mock2ndTaskDetails.TaskCompleted; mockTaskListModel.TaskDescription = mock2ndTaskDetails.TaskDescription; mockTaskListModel.Tasks = mockTaskList; this.mockContext.SetupGet(x => x.Request).Returns(this.mockRequest.Object); this.mockContext.SetupGet(x => x.Response).Returns(this.mockResponse.Object); mockContext.Setup(x => x.Session["InMemoryTaskList"]).Returns(mockTaskListModel); var mockControllerContext = new RequestContext(this.mockContext.Object, new RouteData()); TaskListController controller = new TaskListController(); controller.tasklist = mockTasklist.Object; controller.ControllerContext = new ControllerContext(mockControllerContext, controller); this.mockTasklist.Setup(c => c.GetAllTasks()).Returns(mockTaskList); this.mockTasklist.Setup(c => c.GenerateNextIdNumber()).Returns(2); this.mockTasklist.Setup(c => c.Inserttask(mock2ndTaskDetails.ID, mock2ndTaskDetails.TaskDescription, mock2ndTaskDetails.TaskCompleted)); #endregion #region Act var result = (RedirectToRouteResult)controller.Create(mockTaskListModel); var viewModel = controller.ViewData.Model as TaskListModel; #endregion #region Assert this.mockTasklist.Verify(c => c.GetAllTasks(), Times.Once()); this.mockTasklist.Verify(c => c.GenerateNextIdNumber(), Times.Once()); this.mockTasklist.Verify(c => c.Inserttask(mock2ndTaskDetails.ID, mock2ndTaskDetails.TaskDescription, mock2ndTaskDetails.TaskCompleted), Times.Once()); Assert.AreEqual(2, mock2ndTaskDetails.ID); Assert.IsNotNull(controller.HttpContext.Session["InMemoryTaskList"]); #endregion }