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 = "firstname.lastname@example.org";
NameValueCollection config = new NameValueCollection();
MembershipCreateStatus status = new MembershipCreateStatus();
provider.CreateUser(userName, password, email,null,null,true,null,out 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"/>
<add applicationName="MeanWorm" requiresQuestionAndAnswer="false"
enablePasswordReset="true" passwordFormat="Hashed" connectionStringName="MeanWormConnectionString"
name="MeanWormMembershipProvider" type="MeanWorm.Domain.Providers.MeanWormMembershipProvider,MeanWorm.Domain" />
Now, when you run the test it will pass.