The mechanics of making this work aren't hard, but testing it is a little strange. The scenario is that I want to dump some basic user data into the view data based on the User property of a base controller (an IPrincipal object) so that the master page always has it. I need access to my IUserManager (a service class), which is provided by custom DI in a controller factory. Mocking the user is no problem for testing. However, the easiest way to achieve this for every action in the base controller is to do it by overriding the OnAuthorization method. The base class then looks like this:
public abstract class BaseController : Controller
{
public BaseController(IUserManager userManager)
{
UserManager = userManager;
}
public IUserManager UserManager { get; private set; }
protected override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
UserManager.SetupUserViewData(User, ViewData);
}
}
The problem is that there's no way that I've been able to figure in a test to get the OnAuth method to fire. I'd like to verify in my mock UserManager that the SetupUserViewData gets called. I'm not using a custom filter because I don't have a full-blown dependency injection framework in place (the filter would need to get an IUserManager).
Any suggestions? As this will be used everywhere, I'd like to get the testing right.