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