title: How to Unit Test Asp.net Membership?
body: <p>I am new to unit testing and I am trying to test some of my .NET membership stuff I been writing.</p>
  
  <p>So I am trying to check my <code>VerifyUser</code> method that checks if the users credentials are valid or not. </p>
  
  <p>So this is what it looks like:</p>
  
  <pre><code>public bool VerifyUser(string userName, string password)
      {
          bool valid = Membership.ValidateUser(userName, password);
          return valid;
      }
  </code></pre>
  
  <p>And now every time I run my unit test it fails. I know I am passing in the right credentials and stuff. Then it dawned on me that maybe my Test Project(that is under the same Solution as my real project) might need its own <code>web.config</code> file with the connection string and stuff. Or an app config file maybe since it is a Application Library project.</p>
  
  <p>So do I just copy the <code>web.config</code> file from my real project and call it a day? Or should I only be taking parts from it? Or am I just way off.</p>
  
  <p>My database is using a custom database with the .net membership merged with my database. So in my config file I had to specify a ManagerProvider and a roleProvider.</p>
  
  <p>This is how my unit test looks like</p>
  
  <pre><code>   [Test]
     public void TestVerifyUser()
     {
        AuthenticateUser authenitcate = new AuthenticateUser();
        bool vaild = authenitcate.VerifyUser("chobo3", "1234567");
  
  
        Assert.That(vaild, Is.True);
     }
  </code></pre>
  
  <p>Also later on I have in one of my asp.net mvc ActionResult Methods(the Login View to be exact) I have this:</p>
  
  <p>FormsAuthentication.RedirectFromLoginPage(loginValidation.UserName, rememberMe);</p>
  
  <p>So now how can I write a unit test that would do what a user would do. Say they start at the Home page then click on the login page and log in successfully. I want them to be redirect to the home page.</p>
  
  <p>I am not sure how to represent that in code. I am pretty sure that the <code>RedirectFromLoginPage</code> works and thats now what I am really testing. I am testing the fact that I have 3 things that can happen in the login <code>ActionResult</code> method.</p>
  
  <ol>
  <li>User logs in and gets sent back where they came from.</li>
  <li>User fails to login and gets sent back to the <code>LoginView</code> and sees error messages.</li>
  <li>User has tried to go to a secure and has been redirect to the login page. If the login successfully the will be redirect back to the secure page via the ReturnUrl.</li>
  </ol>
  
  <p>So I want to do a test to see if these work as they should. So thats why I need to have the user coming from like the home page to see if they get redirected back to it later and if they come from a secure page they get redirect back to that later on.</p>
  
  <p>I am also by the way using NUnit 2.5 and VS2008 Pro.</p>
  
  <hr>
  
  <p>This is what I am trying to test. I am at the part where I am trying to see if the user is valid or not(the if statement). I have no clue how to test it.</p>
  
  <pre><code>public ActionResult Login(string returnUrl, FormCollection form, bool rememberMe)
         {
              LoginValidation loginValidation = new LoginValidation();
              try
              {
                  UpdateModel(loginValidation, form.ToValueProvider());
  
              }
              catch
              {
  
                  return View("Login");
              }
  
              if (ModelState.IsValid == true)
              {
  
                  bool valid = authenticate.VerifyUser(loginValidation.UserName, loginValidation.Password);
  
                  if (valid == false)
                  {
                      ModelState.AddModelError("frm_Login", "Either the Password or UserName is invalid");
  
                  }
                  else if (string.IsNullOrEmpty(returnUrl) == false)
                  {
                      /* if the user has been sent away from a page that requires them to login and they do 
                       * login then redirect them back to this area*/
                      return Redirect(returnUrl);
                  }
                  else
                  {
  
                      FormsAuthentication.RedirectFromLoginPage(loginValidation.UserName, rememberMe);
                  }
  
              }
  
  
              return View("Login");
          }
  </code></pre>