I have the override below to do a few things with cookies using the FormsIdentity Object. But when I instantiate a controller on my Unit Testing this method is not triggered. Any ideas how to do this?
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
base.Initialize(requestContext);
// Grab the user's login information from FormsAuth
_userState = new UserState();
if (this.User.Identity != null && this.User.Identity is FormsIdentity)
this._userState.FromString(((FormsIdentity)this.User.Identity).Ticket.UserData);
// have to explicitly add this so Master can see untyped value
this.UserState = _userState;
//this.ViewData["ErrorDisplay"] = this.ErrorDisplay;
// custom views should also add these as properties
}
Controller.Initialize(RequestContext)is invoked insideControllerBase.Execute(RequestContext), which in turn can be invoked by the explicit implementation ofIController.Execute(RequestContext), so this code will initialize and execute a controller:I’ve analyzed the dependency tree for
Initialize()and I can’t see any other way of invoking this method without resorting to Reflection. You will need to create theRequestContextpassed toExecute()as well, which is easier as it looks like you are using Moq:This link has helpful details on mocking an HttpContext using Moq.