Search code examples
c#asp.net-mvcvisual-studionunitrole-manager

Role Manager not enabled. Even after changing web.config


I have enabled role manager in web.config, but am still getting the error:

System.Configuration.Provider.ProviderException : The Role Manager feature has not been enabled.

I am using nunit, and this is my test method

[Test]
public void DownloadbloombergTest()
{
    if (!Roles.RoleExists("Admin"))
        Roles.CreateRole("Admin");
    if (!Roles.IsUserInRole(WebSecurity.CurrentUserName, "Admin"))
        Roles.AddUserToRole(WebSecurity.CurrentUserName, "Admin");    
    var controller = new DownloadBloombergController();
    var result = controller.DownloadBloomberg() as ViewResult;           
    Assert.IsInstanceOf<ViewResult>(result);
}

and this is my web.config

...
<system.web>


     <membership defaultProvider="SimpleMembershipProvider">
      <providers>
        <clear />
        <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
      </providers>
    </membership>
    <roleManager enabled="true" defaultProvider="SimpleRoleProvider">
      <providers>
        <clear />
        <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" />
      </providers>
    </roleManager>


    <customErrors mode="Off" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <authentication mode="Forms">
      <forms loginUrl="~/Login/Login" timeout="2880" />
    </authentication>
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
  </system.web>
...

nunit is pointing to the line (the beginning of the method)

 if (!Roles.RoleExists("Admin"))

Solution

  • Copying the Web.config file to the test class library and renaming it to app.config solved the problem