title: Unit Test Relies on UserManager and RoleManager
body: <p>I am trying to unit test some of my methods that rely on <code>UserManager</code> and <code>RoleManager</code> and am having some difficulty.</p>
  
  <p>Should I mock the <code>UserManager</code> and <code>RoleManager</code> and then pass it to the <code>AdminController</code>? or should I first access the <code>AccountController</code>'s default <code>SignIn</code> action and authenticate. I am unsure how to do both options or what the best way to approach this is.</p>
  
  <p>When not authenticating / instantiating the managers I get NullReferenceExceptions on the <code>UserManager</code></p>
  
  <p><strong>My test</strong></p>
  
  <pre><code>    [Test]
      public void MaxRole_SuperAdmin()
      {
          var adminController = new AdminController();
          var maxRole = adminController.GetMaxRole(SuperAdminUserId);
  
          Assert.AreEqual(maxRole, "Super Admin");
      }
  </code></pre>
  
  <p><strong>Controller and Method</strong></p>
  
  <pre><code>[Authorize(Roles = "APGame Admin, APGame Investigator")]
  [RequireHttps]
  public class AdminController : Controller
  {
  
      private ApplicationUserManager _userManager;
  
      public ApplicationUserManager UserManager
      {
          get { return _userManager ?? HttpContext.GetOwinContext().GetUserManager&lt;ApplicationUserManager&gt;(); }
          private set { _userManager = value; }
      }
  
      private ApplicationRoleManager roleManager;
  
      public ApplicationRoleManager RoleManager
      {
          get
          {
              return roleManager ?? HttpContext.GetOwinContext().Get&lt;ApplicationRoleManager&gt;();
          }
          private set { roleManager = value; }
      }
  
      public string GetMaxRole(string userId)
      {
          IEnumerable&lt;string&gt; userRoles = UserManager.GetRoles(userId);
  
          string role = null;
  
          if (userRoles.Contains("APGame Admin"))
          {
              if (userRoles.Contains("TeVelde Group") &amp;&amp; userRoles.Contains("Genomics Group"))
                  role = "Super Admin";
  
              else role = "Admin";
          }
  
          else if (userRoles.Contains("APGame Investigator"))
          {
              role = "Investigator";
          }
  
          else if (userRoles.Contains("APGame User"))
          {
              role = "User";
          }
  
          else
          {
              //TODO: Log no role, HIGH
          }
  
          return role;
      }
  }
  </code></pre>