2
我有下面的覆盖使用FormsIdentity对象使用cookie做几件事。但是当我在单元测试中实例化一个控制器时,这个方法不会被触发。任何想法如何做到这一点?如何在尝试单元测试时触发初始化方法?
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
}
谢谢,山姆。我能够触发Initialize方法,但仍然出现错误,嘲笑httpContext比我想象的要困难得多。 – Geo 2009-09-22 01:24:58
HttpContext是可测试性的反基督 - 我尽量远离它,我很惊讶Initialize()也是如此弱测试。也许你可以看看使用自定义模型绑定器或属性依赖注入,而不是使用Initialize()呢? – Sam 2009-09-22 01:38:04