public void Index() { MemoryUser user = new MemoryUser("adarsh"); ApplicationUser applicationUser = new ApplicationUser() { Activated = true, Email = "*****@*****.**", FirstName = "Adarsh", LastName = "Vikraman", RoleId = 0 }; var userContext = new UserInfo { UserId = user.Id, DisplayName = user.UserName, UserIdentifier = applicationUser.Email, RoleName = Enum.GetName(typeof(UserRoles), applicationUser.RoleId) }; GoalController controller = new GoalController(goalService, metricService, focusService, supportService, updateService, commentService, userService, securityTokenService, supportInvitationService, goalStatusService, commentUserService, updateSupportService); var testTicket = new FormsAuthenticationTicket( 1, user.Id, DateTime.Now, DateTime.Now.Add(FormsAuthentication.Timeout), false, userContext.ToString()); principal.SetupGet(x => x.Identity.Name).Returns("adarsh"); controllerContext.SetupGet(x => x.HttpContext.User).Returns(principal.Object); controllerContext.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true); controller.ControllerContext = controllerContext.Object; contextBase.SetupGet(x => x.Request).Returns(httpRequest.Object); contextBase.SetupGet(x => x.Response).Returns(httpResponse.Object); genericPrincipal.Setup(x => x.Identity).Returns(identity.Object); contextBase.SetupGet(a => a.Response.Cookies).Returns(new HttpCookieCollection()); var formsAuthentication = new DefaultFormsAuthentication(); formsAuthentication.SetAuthCookie(contextBase.Object, testTicket); HttpCookie authCookie = contextBase.Object.Response.Cookies[FormsAuthentication.FormsCookieName]; var ticket = formsAuthentication.Decrypt(authCookie.Value); var goalsetterUser = new SocialGoalUser(ticket); string[] userRoles = { goalsetterUser.RoleName }; principal.Setup(x => x.Identity).Returns(goalsetterUser); Goal goal = new Goal() { GoalId = 1, GoalName = "t", GoalStatusId = 1, Desc = "x", StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(1) }; goalRepository.Setup(x => x.GetById(1)).Returns(goal); IEnumerable<GoalStatus> fake = new List<GoalStatus> { new GoalStatus { GoalStatusId =1, GoalStatusType ="Inprogress"}, new GoalStatus { GoalStatusId =2, GoalStatusType ="OnHold"}, }.AsEnumerable(); goalStatusRepository.Setup(x => x.GetAll()).Returns(fake); Mapper.CreateMap<Goal, GoalViewModel>(); ViewResult result = controller.Index(1) as ViewResult; Assert.IsNotNull(result); Assert.IsInstanceOfType(typeof(GoalViewModel), result.ViewData.Model, "WrongType"); var data = result.ViewData.Model as GoalViewModel; Assert.AreEqual("t", data.GoalName); }