public void Register_Get_ReturnsView()
        {
            // Arrange
            var memberService = MockRepository.GenerateStub<IMembershipService>();
            memberService.Stub(x => x.MinPasswordLength).Return(10);

            var controller = new AccountController { MembershipService = memberService };

            ///AccountController controller = GetAccountController();

            // Act
            ActionResult result = controller.Register();

            // Assert
            Assert.IsInstanceOfType(result, typeof(ViewResult));
            Assert.AreEqual(10, ((ViewResult)result).ViewData["PasswordLength"]);
        }
        public void LogOff_LogsOutAndRedirects()
        {
            // Arrange
            var formService = MockRepository.GenerateMock<IFormsAuthenticationService>();
            formService.Expect(x => x.SignOut());
            var controller = new AccountController {FormsService = formService};

            //AccountController controller = GetAccountController();

            // Act
            ActionResult result = controller.LogOff();

            // Assert
            Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
            RedirectToRouteResult redirectResult = (RedirectToRouteResult)result;
            Assert.AreEqual("Home", redirectResult.RouteValues["controller"]);
            Assert.AreEqual("Index", redirectResult.RouteValues["action"]);
            formService.AssertWasCalled(x => x.SignOut());
            //Assert.IsTrue(((MockFormsAuthenticationService)controller.FormsService).SignOut_WasCalled);
        }
 private static AccountController GetAccountController()
 {
     RequestContext requestContext = new RequestContext(new MockHttpContext(), new RouteData());
     AccountController controller = new AccountController()
     {
         FormsService = new MockFormsAuthenticationService(),
         MembershipService = new MockMembershipService(),
         Url = new UrlHelper(requestContext),
     };
     controller.ControllerContext = new ControllerContext()
     {
         Controller = controller,
         RequestContext = requestContext
     };
     return controller;
 }
        public void Register_Post_ReturnsRedirectOnSuccess()
        {
            // Arrange
            var memberService = MockRepository.GenerateStub<IMembershipService>();
            memberService.Stub(x => x.MinPasswordLength).Return(10);
            memberService.Stub(x => x.CreateUser("abc", "def", "ijk")).Return(
                MembershipCreateStatus.Success);

            var formService = MockRepository.GenerateMock<IFormsAuthenticationService>();
            formService.Expect(x => x.SignIn("someUser", false));

            var controller = new AccountController {FormsService = formService, MembershipService = memberService};
            //AccountController controller = GetAccountController();
            RegisterModel model = new RegisterModel()
            {
                UserName = "******",
                Email = "goodEmail",
                Password = "******",
                ConfirmPassword = "******"
            };

            // Act
            ActionResult result = controller.Register(model);

            // Assert
            Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
            RedirectToRouteResult redirectResult = (RedirectToRouteResult)result;
            Assert.AreEqual("Home", redirectResult.RouteValues["controller"]);
            Assert.AreEqual("Index", redirectResult.RouteValues["action"]);
            //formService.AssertWasCalled(x => x.SignIn("someUser", false));
            formService.VerifyAllExpectations();
        }
        public void Register_Post_ReturnsViewIfRegistrationFails()
        {
            // Arrange
            var memberService = MockRepository.GenerateStub<IMembershipService>();
            memberService.Stub(x => x.MinPasswordLength).Return(10);
            memberService.Stub(x => x.CreateUser("abc", "def", "ijk")).IgnoreArguments().Return(
                MembershipCreateStatus.DuplicateUserName);
            var controller = new AccountController {MembershipService = memberService};

            //AccountController controller = GetAccountController();
            RegisterModel model = new RegisterModel()
            {
                UserName = "******",
                Email = "goodEmail",
                Password = "******",
                ConfirmPassword = "******"
            };

            // Act
            ActionResult result = controller.Register(model);

            // Assert
            Assert.IsInstanceOfType(result, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)result;
            Assert.AreEqual(model, viewResult.ViewData.Model);
            Assert.AreEqual("Username already exists. Please enter a different user name.", controller.ModelState[""].Errors[0].ErrorMessage);
            Assert.AreEqual(10, viewResult.ViewData["PasswordLength"]);
        }