Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

Sometimes back I wrote a blog post about Unit Testing Membership Providers. Yesterday, I had to do the same hence I turned back to my post but unfortunately I could not figure it out. After banging my head for several hours I finally figured out the problem. This post describes everything you need to do in order to unit test your providers.

First create the custom provider which you would like to test. Here is my custom provider. My custom provider is created in a separate class library project.

public class MeanWormMembershipProvider : SqlMembershipProvider
    {
      
    }

In your class library (Unit Test Project) you will refer to your favorite unit testing dll. In my case I am referring to MbUnit. And here is my test.

 [TestFixture]
    public class TestRegistration
    {
        [Test]
        [RollBack]
        public void TestCanAddNewUser()
        {
            MeanWormMembershipProvider provider = new MeanWormMembershipProvider();
          
            string userName = "marykate2";
            string password = "marykate$";
            string email = "marykate2@gmail.com";

            NameValueCollection config = new NameValueCollection();
            config.Add("applicationName", "MeanWorm");
            config.Add("name", "MeanWormMembershipProvider");
            config.Add("requiresQuestionAndAnswer", "false");
            config.Add("connectionStringName", "MeanWormConnectionString");
            provider.Initialize(config["name"], config);

            MembershipCreateStatus status = new MembershipCreateStatus();
           
            provider.CreateUser(userName, password, email,null,null,true,null,out status);

            Assert.AreEqual(MembershipCreateStatus.Success, status);
        }
    }

If you run the test it will fail and throw the exception "membership provider name is invalid". That is because you have not yet created the configurations in the App.config file. So, add a App.config file in your test project and insert the following code.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="MeanWormConnectionString" connectionString="Server=localhost;Database=MeanWormDB;Trusted_Connection=true"/>
  </connectionStrings>

  <system.web>

    <membership defaultProvider="MeanWormMembershipProvider">
      <providers>
        <remove name="AspNetSqlMembershipProvider"/>
       
          <add applicationName="MeanWorm" requiresQuestionAndAnswer="false"
            requiresUniqueEmail="true" minRequiredNonalphanumericCharacters="0"
            enablePasswordReset="true" passwordFormat="Hashed" connectionStringName="MeanWormConnectionString"
            name="MeanWormMembershipProvider" type="MeanWorm.Domain.Providers.MeanWormMembershipProvider,MeanWorm.Domain" />
       
      </providers>
    </membership>
  </system.web>
 
</configuration>

Now, when you run the test it will pass.  

Posted on Sunday, December 30, 2007 10:22 AM | Back to top


Comments on this post: Membership Provider and Unit Testing

# re: Membership Provider and Unit Testing
Requesting Gravatar...
After I created and ran your sample I tried to call the static GetUser() method in the Membership class. I made sure to override the GetUser(string name, bool isOnline) method in my provider. The only problem is that for some reason when the overriden GetUser method gets called the username paramater is empty... I am setting the username correctly in the constructor of my derived MembershipUser class... I am a little stumped now. Any ideas?
Left by Noam on Feb 01, 2008 8:07 AM

# re: Membership Provider and Unit Testing
Requesting Gravatar...
Great Post! Thanks a lot. That helped!
Left by Sascha on Jun 05, 2008 2:03 AM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net | Join free