title: Why does my asp.net mvc controller not call it's base class' Initialize method during unit tests?
body: <p>I am attempting to unit test my controllers, and I am using the default MVC 3 <code>AccountController</code> unit tests as a basis.  So far I have my controller, which looks like:</p>
  
  <pre><code>public partial class HomeController : MyBaseController
  {
      public HomeController(IUnitOfWork unitOfWork) { _unitOfWork = unitOfWork; }
  
      public virtual ActionResult Index()
      {
  
          return View();
      }
  
      public virtual ActionResult About()
      {
          return View();
      }
  }
  </code></pre>
  
  <p><code>MyBaseController</code> has the following code:</p>
  
  <pre><code>public class MyBaseController : Controller
  {
      public MyJobLeadsBaseController()
      {
          CurrentUserId = 0;
      }
  
      protected override void Initialize(RequestContext requestContext)
      {
          if (MembershipService == null) { MembershipService = new AccountMembershipService(); }
          base.Initialize(requestContext);
  
          // If the user is logged in, retrieve their login id
          var user = MembershipService.GetUser();
          if (user != null)
              CurrentUserId = (int)user.ProviderUserKey;
      }
  
      public int CurrentUserId { get; set; }
      public IMembershipService MembershipService { get; set; }
  
      protected IUnitOfWork _unitOfWork;
  }
  </code></pre>
  
  <p>Everything works correctly when I run the actual site, and breakpoints show that the <code>Initialize()</code> is correctly being triggered.  However, the following unit test never runs the <code>Initialize(RequestContext)</code> method:</p>
  
  <pre><code>    [TestMethod]
      public void Index_Shows_When_Not_Logged_In()
      {
          // Setup
          HomeController controller = new HomeController(_unitOfWork);
          controller.MembershipService = new MockMembershipService(null);
          SetupController(controller);
  
          // Act
          ActionResult result = controller.Index();
  
          // Verify
          Assert.IsNotNull(result, "Index returned a null action result");
          Assert.IsInstanceOfType(result, typeof(ViewResult), "Index did not return a view result");
      }
  
      protected static void SetupController(Controller controller)
      {
          RequestContext requestContext = new RequestContext(new MockHttpContext(), new RouteData());
          controller.Url = new UrlHelper(requestContext);
  
          controller.ControllerContext = new ControllerContext
          {
              Controller = controller,
              RequestContext = requestContext
          };
      }
  </code></pre>
  
  <p>Debugging through this unit test shows that at no point does the overridden <code>MyBaseController.Initialize()</code> get called at all.  This causes issues where my <code>CurrentUserId</code> property is not being set in unit tests, but is being set on the live system.</p>
  
  <p>What else do I have to do to trigger the <code>Initialize()</code> to be called?</p>