title: NUnit Test for ActionResult which checks user roles
body: <p>I am currently new to ASPNET MVC framework and I wanted to do some unit testing using NUnit, and I am having difficulties. </p>
  
  <p>My issue is that I have a controller and most of the <code>ActionResults</code> methods have got checks inside them checking whether the current login user belongs to certain roles, now my issue is how do I log in using <code>NUnit</code> so those <code>ActionResults</code> tests can be carries, otherwise they are returning <code>nullexception</code>. </p>
  
  <p>The following is some of the code for the <code>Index()</code> Method to give you a overview of the situation. Could you also possibly point out what kind of things I can test in this?</p>
  
  <pre><code>public ActionResult Index()
  {
        IEnumerable&lt;Colour&gt; colours = null;
  
        if (isUserAdmin())
        {
            colours = db.Colours;
        }
        else
        {
            string userId = User.Identity.GetUserId();
            IEnumerable&lt;string&gt; userRoleIds = db.Users.Find(userId).Roles.Select(x =&gt; x.Id);
            colours = db.ColourRoles.Where(x =&gt; userRoleIds.Contains(x.RoleId)).Select(y =&gt; y.Colour).Distinct();
        }
  
        ViewData["Colours"] = colours;            
        return View();
  } 
  </code></pre>
  
  <p>Thank you in advance. </p>