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

Some time back I blogged about unit testing Membership Provider "Membership Provider and Unit Testing" as it turned out I was missing the unit tests for Role Provider. In this post I will explain how to unit test the Role Provider.

First of all here is the implementation of the role provider.

 public class VirtualRoomRoleProvider : SqlRoleProvider
    {
        private NameValueCollection _roleConfig = new NameValueCollection();

        public VirtualRoomRoleProvider()    
        {           
            _roleConfig.Add("applicationName", "VirtualRoom");           
            _roleConfig.Add("connectionStringName", "VirtualRoomConnectionString");
            this.Initialize("VirtualRoomRoleProvider", _roleConfig);
           
        } 
    }

Inside the constructor I am initializing the RoleProvider.

Now, here is the App.config which is inside the TestSuite class library.

  <system.web>

    <roleManager enabled="true" defaultProvider="VirtualRoomRoleProvider" cacheRolesInCookie="true"
                  cookieName="VirtualRoomCookie">
      <providers>
        <add name="VirtualRoomRoleProvider" type="System.Web.Security.SqlRoleProvider"
              connectionStringName="VirtualRoomConnectionString" applicationName="VirtualRoom"/>
      </providers>
     
    </roleManager>
    <membership defaultProvider="VirtualRoomProvider">
      <providers>
        <remove name="AspNetSqlMembershipProvider"/>

        <add applicationName="VirtualRoom" requiresQuestionAndAnswer="false"
          requiresUniqueEmail="true" minRequiredNonalphanumericCharacters="0"
          enablePasswordReset="true" passwordFormat="Hashed" connectionStringName="VirtualRoomConnectionString"
          name="VirtualRoomProvider" type="DomainObjects.Providers.VirtualRoomProvider,DomainObjects" />

      </providers>
    </membership>
  </system.web>

And finally here are the tests.

 [TestFixture]
    public class when_a_user_is_assigned_a_role : BaseTest
    {
        [Test]
        public void should_be_able_to_initialize_role_provider()
        {
            VirtualRoomRoleProvider roleProvider = new VirtualRoomRoleProvider();

            Assert.AreEqual("VirtualRoom", roleProvider.ApplicationName);
            Assert.AreEqual("VirtualRoomRoleProvider", roleProvider.Name);
        }

        [Test]
        public void should_verify_that_the_roles_exists()
        {
            Assert.IsTrue(_roleProvider.GetAllRoles().Count() > 0,"No roles exists");
        }

        [Test]
        [RollBack]
        public void should_assign_the_student_role_to_the_new_user()
        {
            MembershipUser user = _provider.CreateUser(_username, _password, _email, null, null, true, null, out _status);

            Assert.IsTrue(_roleProvider.IsUserInRole(user.UserName, "Student"),"User is not a student!");
        }
}

The BaseTest class does all the work to initialize the variables.

 [TestFixture]
    public class BaseTest
    {
        protected string _username;
        protected string _password;
        protected string _email;
        protected VirtualRoomProvider _provider;
        protected NameValueCollection _config;
        protected MembershipCreateStatus _status = new MembershipCreateStatus();
        protected SqlRoleProvider _roleProvider;
        protected NameValueCollection _roleConfig;

        [SetUp]
        public void initialize()
        {
            _username = "mary";
            _password = "marykate$";
            _email = "marykate@gmail.com";

            // setup the membership provider
            _provider = new VirtualRoomProvider();

            _config = new NameValueCollection();
            _config.Add("applicationName", "VirtualRoom");
            _config.Add("name", "VirtualRoomProvider");
            _config.Add("requiresQuestionAndAnswer", "false");
            _config.Add("connectionStringName", "VirtualRoomConnectionString");
            _provider.Initialize(_config["name"], _config);

            _status = new MembershipCreateStatus();

            _roleProvider = new SqlRoleProvider();
            // setup the role provider
            _roleConfig = new NameValueCollection();
            _roleConfig.Add("applicationName", "VirtualRoom");
           // _roleConfig.Add("name", "VirtualRoomRoleProvider");
            _roleConfig.Add("connectionStringName", "VirtualRoomConnectionString");
            _roleProvider.Initialize("VirtualRoomRoleProvider", _roleConfig);

            Console.WriteLine("Setup is fired");
        }
    }

The BaseTest class allows me to use reuse the variables in other classes and tests.

Posted on Tuesday, February 12, 2008 8:48 AM | Back to top


Comments on this post: Unit Testing Role Providers in ASP.NET

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net