title: How to write a test for accounts controller for forms authenticate
body: <p>Trying to figure out how to adequately test my accounts controller.  I am having problem testing the successful logon scenario.</p>
  
  <p>Issue 1)  Am I missing any other tests.(I am testing the model validation attributes separately)</p>
  
  <p>Issue 2) Put_ReturnsOverviewRedirectToRouteResultIfLogonSuccessAndNoReturnUrlGiven() and Put_ReturnsRedirectResultIfLogonSuccessAndReturnUrlGiven() test are not passing.  I have narrowed it down to the line where i am calling _membership.validateuser().  Even though during my mock setup of the service i am stating that i want to return true whenever validateuser is called, the method call returns false.</p>
  
  <p>Here is what I have gotten so far</p>
  
  <p>AccountController.cs</p>
  
  <pre><code>[HandleError]
  public class AccountController : Controller
  {
      private IMembershipService _membershipService;
  
      public AccountController()
          : this(null)
      {
      }
  
      public AccountController(IMembershipService membershipService)
      {
          _membershipService = membershipService ?? new AccountMembershipService();
      }
  
      [HttpGet]
      public ActionResult LogOn()
      {
          return View();
      }
  
      [HttpPost]
      public ActionResult LogOn(LogOnModel model, string returnUrl)
      {
          if (ModelState.IsValid)
          {
              if (_membershipService.ValidateUser(model.UserName,model.Password))
              {
                  if (!String.IsNullOrEmpty(returnUrl))
                  {
                      return Redirect(returnUrl);
                  }
                  return RedirectToAction("Index", "Overview");
              }
              ModelState.AddModelError("*", "The user name or password provided is incorrect.");
          }
  
          return View(model);
      }
  
  }
  </code></pre>
  
  <p>AccountServices.cs  </p>
  
  <pre><code>public interface IMembershipService
  {
      bool ValidateUser(string userName, string password);
  }
  
  public class AccountMembershipService : IMembershipService
  {
      public bool ValidateUser(string userName, string password)
      {
          throw new System.NotImplementedException();
      }
  }
  </code></pre>
  
  <p>AccountControllerFacts.cs  </p>
  
  <pre><code>public class AccountControllerFacts
  {
      public static AccountController GetAccountControllerForLogonSuccess()
      {
          var membershipServiceStub = MockRepository.GenerateStub&lt;IMembershipService&gt;();
          var controller = new AccountController(membershipServiceStub);
          membershipServiceStub
                 .Stub(x =&gt; x.ValidateUser("someuser", "somepass"))
                 .Return(true);
          return controller;
      }
  
      public static AccountController GetAccountControllerForLogonFailure()
      {
          var membershipServiceStub = MockRepository.GenerateStub&lt;IMembershipService&gt;();
          var controller = new AccountController(membershipServiceStub);
          membershipServiceStub
                 .Stub(x =&gt; x.ValidateUser("someuser", "somepass"))
                 .Return(false);
          return controller;
      }
  
      public class LogOn
      {
          [Fact]
          public void Get_ReturnsViewResultWithDefaultViewName()
          {
              // Arrange
              var controller = GetAccountControllerForLogonSuccess();
  
              // Act
              var result = controller.LogOn();
  
              // Assert
              Assert.IsType&lt;ViewResult&gt;(result);
              Assert.Empty(((ViewResult)result).ViewName);
          }
  
          [Fact]
          public void Put_ReturnsOverviewRedirectToRouteResultIfLogonSuccessAndNoReturnUrlGiven()
          {
              // Arrange
              var controller = GetAccountControllerForLogonSuccess();
              var user = new LogOnModel();
  
              // Act
              var result = controller.LogOn(user, null);
              var redirectresult = (RedirectToRouteResult) result;
  
              // Assert
              Assert.IsType&lt;RedirectToRouteResult&gt;(result);
              Assert.Equal("Overview", redirectresult.RouteValues["controller"]);
              Assert.Equal("Index", redirectresult.RouteValues["action"]);
          }
  
          [Fact]
          public void Put_ReturnsRedirectResultIfLogonSuccessAndReturnUrlGiven()
          {
              // Arrange
              var controller = GetAccountControllerForLogonSuccess();
              var user = new LogOnModel();
  
              // Act
              var result = controller.LogOn(user, "someurl");
              var redirectResult = (RedirectResult) result;
  
              // Assert
              Assert.IsType&lt;RedirectResult&gt;(result);
              Assert.Equal("someurl", redirectResult.Url);
          }
  
          [Fact]
          public void Put_ReturnsViewIfInvalidModelState()
          {
              // Arrange
              var controller = GetAccountControllerForLogonFailure();
              var user = new LogOnModel();
              controller.ModelState.AddModelError("*","Invalid model state.");
  
              // Act
              var result = controller.LogOn(user, "someurl");
              var viewResult = (ViewResult) result;
  
              // Assert
              Assert.IsType&lt;ViewResult&gt;(result);
              Assert.Empty(viewResult.ViewName);
              Assert.Same(user,viewResult.ViewData.Model);
          }
  
          [Fact]
          public void Put_ReturnsViewIfLogonFailed()
          {
              // Arrange
  
              var controller = GetAccountControllerForLogonFailure();
              var user = new LogOnModel();
  
              // Act
              var result = controller.LogOn(user, "someurl");
              var viewResult = (ViewResult) result;
  
              // Assert
              Assert.IsType&lt;ViewResult&gt;(result);
              Assert.Empty(viewResult.ViewName);
              Assert.Same(user,viewResult.ViewData.Model);
              Assert.Equal(false,viewResult.ViewData.ModelState.IsValid);
          }
      }
  }
  </code></pre>