Geeks With Blogs

  • azamsharp The WWDC app says that there are new videos but there is nothing under videos!!! about 658 days ago
  • azamsharp Can I post my opinion on iOS 7 Beta or is it under NDA or something? about 658 days ago
  • azamsharp iOS 7 BETA installed successfully! about 658 days ago
  • azamsharp iOS 7 BETA installed! Now restoring! I am on NET10 which uses AT&T network. Hopefully 3G will still work! Lets c.. about 658 days ago
  • azamsharp The missing of button borders in Xcode 5 makes it hard to determine the clickable area. about 659 days ago
  • azamsharp I might wait for iOS to be released before I can put it on my device! Don't want a bricked or unstable device! about 659 days ago
  • azamsharp Android has many features that no one uses, number of versions that no one upgrades to and thousands of apps that no one pays for. about 659 days ago
  • azamsharp Maybe it is just me but the new iOS 7 UI looks like Windows Phone 8 UI! #maybeIamCrazy about 659 days ago
  • azamsharp Anyone using H20 network upgraded to iOS 7 BETA Keep me updated if you face any problems. about 659 days ago
  • azamsharp @merowing_ You already downloaded it! I cannot even load the developer's website! :( about 659 days ago

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.

    public class TestRegistration
        public void TestCanAddNewUser()
            MeanWormMembershipProvider provider = new MeanWormMembershipProvider();
            string userName = "marykate2";
            string password = "marykate$";
            string email = "";

            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" ?>
    <add name="MeanWormConnectionString" connectionString="Server=localhost;Database=MeanWormDB;Trusted_Connection=true"/>


    <membership defaultProvider="MeanWormMembershipProvider">
        <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" />

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: | Join free