title: How do I unit test a controller method that has the [Authorize] attribute applied?
body: <p>I've searched stackoverflow and googled four a couple of hours and still not found any solution for my "trivial" problem.</p>
  
  <p>If you write unit test for your filtered <code>[Authorize] ActionResult</code>, how do you solve the problem to fake that user is authenticated?</p>
  
  <p>I have a lot of <code>ActionResult</code> methods that are filtered with <code>[Authorize]</code> and I want to test all of my <code>ActionResult</code> methods regardless if they are filtered with <code>[Authorize]</code> or not.</p>
  
  <p>A simple example of what i mean:</p>
  
  <pre><code>[TestMethod]
  public void Create_Get_ReturnsView()
  {
   // Arrange
   var controller = new UserController();
   // Act
   var result = controller.Create();
   // Assert
   Assert.IsNotNull(result as ViewResult);
  }
  
  [Authorize]
  public ActionResult Create()
  {
   return View("Create");
  }
  </code></pre>
  
  <p>As of now the tests don't even hit the ActionResult method because of the  [Authorize] filter, exception thrown is:  <code>System.NullReferenceException: Object reference not set to an instance of an object.</code></p>