title: Unit Test for login in asp.net mvc 3
body: <p>I have a problem testing a logon method using existing AccountController (made by MVC)...</p>
  
  <p>I have this simple test method:</p>
  
  <pre><code>  [TestMethod]
    public void LogOnTest1() {
       AccountController controller = new AccountController();
  
       LogOnModel logonModel = new LogOnModel();
       logonModel.UserName = "test";
       logonModel.Password = "test1234";
  
       if ( controller.MembershipService == null ) {
          controller.MembershipService = new AccountMembershipService();
       }
  
       if ( controller.FormsService == null ) {
          controller.FormsService = new FormsAuthenticationService();
       }
  
       var result = controller.LogOn( logonModel, "" ) as ViewResult;
  
       Assert.AreEqual( "Index", result.ViewName );
    }
  </code></pre>
  
  <p>and the method defined in <code>AccountController</code>:</p>
  
  <pre><code>[HttpPost]
        public ActionResult LogOn( LogOnModel model, string returnUrl ) {
           if ( ModelState.IsValid ) {
              if ( MembershipService.ValidateUser( model.UserName, model.Password ) ) {
                 FormsService.SignIn( model.UserName, model.RememberMe );
                 if ( !string.IsNullOrEmpty( returnUrl ) ) {
                    return Redirect( returnUrl );
                 } else {
                    return RedirectToAction( "Index", "Home" );
                 }
              } else {
                 ModelState.AddModelError( "", "The user name or password provided is incorrect." );
              }
           }
  
           // If we got this far, something failed, redisplay form
           return View( model );
        }
  </code></pre>
  
  <p>The above method is not defined/modified by me. Just created by when create an asp.net mvc project.</p>
  
  <p>The problem is at line </p>
  
  <p><code>if ( MembershipService.ValidateUser( model.UserName,model.Password ) ) {</code></p>
  
  <p>which returns <strong>always</strong> false though I provided correct login info.</p>
  
  <p>Where is my mistake ?</p>