public void Cannot_Get_Tasks_By_Invalid_Date() { Mock<ITaskRepository> mock = new Mock<ITaskRepository>(); UserProfile testUser = new UserProfile { UserId = 1, UserName = "******" }; var contextMock = new Mock<ControllerContext>(); //we mock an http context, so we can set our User Identity DateTime yesterday = new DateTime(2013, 8, 5); DateTime dayBeforeYesterday = new DateTime(2013, 8, 4); //for testing an invalid search query mock.Setup(m => m.Tasks).Returns(new Task[] { new Task {ID = 1, Description = "T1", UserProfile = testUser, StartTime = new DateTime(2013, 8, 6), EndTime = new DateTime(2013, 8, 6).AddHours(0.5) }, new Task {ID = 2, Description = "T2", UserProfile = testUser, StartTime = new DateTime(2013, 8, 6), EndTime = new DateTime(2013, 8, 6).AddHours(1) }, new Task {ID = 3, Description = "T3", UserProfile = testUser, StartTime = DateTime.MinValue, EndTime = DateTime.MinValue.AddHours(1) } }.AsQueryable()); contextMock.SetupGet(p => p.HttpContext.User.Identity.Name).Returns("brad-greene"); contextMock.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true); TaskController target = new TaskController(mock.Object); target.ControllerContext = contextMock.Object; //this will authenticate a user named brad-greene String result = (target.TasksByDate(yesterday, dayBeforeYesterday).ViewName); Assert.AreEqual("TasksByDate", result); //if the validation fails then we send the user back to the tasks by date view }
public void Can_Get_Tasks_By_Date() { Mock<ITaskRepository> mock = new Mock<ITaskRepository>(); UserProfile testUser = new UserProfile { UserId = 1, UserName = "******" }; var contextMock = new Mock<ControllerContext>(); //we mock an http context, so we can set our User Identity DateTime yesterday = new DateTime(2013, 8, 5); mock.Setup(m => m.Tasks).Returns(new Task[] { new Task {ID = 1, Description = "T1", UserProfile = testUser, StartTime = new DateTime(2013, 8, 6), EndTime = new DateTime(2013, 8, 6).AddHours(0.5) }, new Task {ID = 2, Description = "T2", UserProfile = testUser, StartTime = new DateTime(2013, 8, 6), EndTime = new DateTime(2013, 8, 6).AddHours(1) }, new Task {ID = 3, Description = "T3", UserProfile = testUser, StartTime = DateTime.MinValue, EndTime = DateTime.MinValue.AddHours(1) } }.AsQueryable()); contextMock.SetupGet(p => p.HttpContext.User.Identity.Name).Returns("brad-greene"); contextMock.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true); TaskController target = new TaskController(mock.Object); target.ControllerContext = contextMock.Object; //this will authenticate a user named brad-greene Task[] result = ((IEnumerable<Task>)target.TasksByDate(yesterday, null).ViewData.Model).ToArray(); Assert.AreEqual(2, result.Length); Assert.AreEqual(1, result[0].ID); Assert.AreEqual(2, result[1].ID); }