public void TestChangePassword() { string testUserName = "******"; string testOldPassword = "******"; string testNewPassword = "******"; var changePasswordModel = new ChangePasswordModel { OldPassword = testOldPassword, NewPassword = testNewPassword }; var accountController = new AccountController(); //Stub HttpContext var stubHttpContext = new StubHttpContextBase(); //Setup ControllerContext so AccountController will use our stubHttpContext accountController.ControllerContext = new ControllerContext(stubHttpContext, new RouteData(), accountController); //Stub IPrincipal var principal = new StubIPrincipal(); principal.IdentityGet = () => { var identity = new StubIIdentity { NameGet = () => testUserName }; return identity; }; stubHttpContext.UserGet = () => principal; RedirectToRouteResult redirectToRouteResult; //Scope the detours we're creating using (ShimsContext.Create()) { ShimMembership.GetUserStringBoolean = (identityName, userIsOnline) => { Assert.AreEqual(testUserName, identityName); Assert.AreEqual(true, userIsOnline); var memberShipUser = new ShimMembershipUser(); //Sets up a detour for MemberShipUser.ChangePassword to our mocked implementation memberShipUser.ChangePasswordStringString = (oldPassword, newPassword) => { Assert.AreEqual(testOldPassword, oldPassword); Assert.AreEqual(testNewPassword, newPassword); return true; }; return memberShipUser; }; var actionResult = accountController.ChangePassword(changePasswordModel); Assert.IsInstanceOf(typeof(RedirectToRouteResult), actionResult); redirectToRouteResult = actionResult as RedirectToRouteResult; } Assert.NotNull(redirectToRouteResult); Assert.AreEqual("ChangePasswordSuccess", redirectToRouteResult.RouteValues["Action"]); }
public void Index_Not_Authenticated() { // Arrange: using (ShimsContext.Create()) { using (HomeController controller = new HomeController()) { StubHttpContextBase stubHttpContext = new StubHttpContextBase(); controller.ControllerContext = new ControllerContext(stubHttpContext, new RouteData(), controller); // Shim the HttpContext.Current var httpRequest = new HttpRequest("", "http://localhost", ""); var httpContext = new HttpContext(httpRequest, new HttpResponse(new StringWriter())); var applicationState = httpContext.Application; ShimHttpContext.CurrentGet = () => httpContext; // Stub the Current.User StubIPrincipal principal = new StubIPrincipal(); principal.IdentityGet = () => { return(new StubIIdentity { NameGet = () => "antonio", IsAuthenticatedGet = () => false }); }; stubHttpContext.UserGet = () => principal; // Act: ActionResult result = controller.Index(); // Assert: Assert.IsNotNull(result); } } }