title: Unit test Custom membership provider with NUnit throws null reference error
body: <p>Trying to create a user account in a test. But getting a Object reference is not set to an instanve of an object error when running it.</p>
  
  <p>Here's my MemberShip provider class, it's in a class library MyCompany.MyApp.Domain.dll:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Web.Security;
  
  namespace MyCompany.MyApp.Domain
  {
      public class MyMembershipProvider : SqlMembershipProvider
      {
          const int defaultPasswordLength = 8;
          private int resetPasswordLength;
  
          public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
          {
              resetPasswordLength = defaultPasswordLength;
              string resetPasswordLengthConfig = config["resetPasswordLength"];
              if (!String.IsNullOrEmpty(resetPasswordLengthConfig))
              {
                  config.Remove("resetPasswordLength");
  
                  if (!int.TryParse(resetPasswordLengthConfig, out resetPasswordLength))
                  {
                      resetPasswordLength = defaultPasswordLength;
                  }
              }
              base.Initialize(name, config);
          }
  
          public override string GeneratePassword()
          {
              return Utils.PasswordGenerator.GeneratePasswordAsWord(resetPasswordLength);
          }
      }
  }
  </code></pre>
  
  <p>Here's my App.Config for my seperate Test Class Library MyCompany.MyApp.Doman.Test.dll that references my business domain library above:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;configuration&gt;
      &lt;connectionStrings&gt;
          &lt;add name="SqlServer" connectionString="data source=mycomp\SQL2008;Integrated Security=SSPI;Initial Catalog=myDatabase" providerName="System.Data.SqlClient"/&gt;
      &lt;/connectionStrings&gt;
      &lt;system.web&gt;
          &lt;membership defaultProvider="MyMembershipProvider" userIsOnlineTimeWindow="15"&gt;
              &lt;providers&gt;
                  &lt;clear/&gt;
                  &lt;add name="MyMembershipProvider"
                       type="MyCompany.MyApp.Domain.MyMembershipProvider,MyCompany.MyApp.Domain"
                       connectionStringName="SqlServer"
                       applicationName="MyApp"
                       minRequiredNonalphanumericCharacters="0"
                       enablePasswordRetrieval="false"
                       enablePasswordReset="true"
                       requiresQuestionAndAnswer="false"
                       requiresUniqueEmail="true"
                       passwordFormat="Hashed"/&gt;
              &lt;/providers&gt;
          &lt;/membership&gt;
      &lt;/system.web&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>Here's my method that throws "Object reference is not set to an instanve of an object"</p>
  
  <pre><code>public class MemberTest
      {
          public static void CreateAdminMemberIfNotExists()
          {
              MembershipCreateStatus status;
              status = MembershipCreateStatus.ProviderError;
  
              MyMembershipProvider provider = new MyMembershipProvider();
  
              provider.CreateUser("Admin", "password", "someone@somewhere.co.uk", "Question", "Answer", true, Guid.NewGuid(), out status);
          }
      }
  </code></pre>
  
  <p>it throws on the provider.CreateUser line</p>