運行我的單元測試方法時,我在HttpSessionStateBase的實例中獲取了空值。我曾經嘲笑像這樣在HttpSessionStateBase中獲取null c#單元測試
var httpRequest = new HttpRequest("", "http://localhost/", "");
var stringWriter = new StringWriter();
var httpResponse = new HttpResponse(stringWriter);
var httpContext = new HttpContext(httpRequest, httpResponse);
var sessionContainer = new HttpSessionStateContainer("id",
new SessionStateItemCollection(),
new HttpStaticObjectsCollection(),
10,
true,
HttpCookieMode.AutoDetect,
SessionStateMode.InProc,
false);
SessionStateUtility.AddHttpSessionStateToContext(httpContext, sessionContainer);
的HttpContext請提出了一些想法,讓在HttpSessionStateBase一些虛擬值
什麼是'SessionStateUtility.AddHttpSessionStateToContext',你到底在哪裏得到空值?請說明你如何試圖訪問你試圖單元測試的實際代碼中的Session對象。 –
public ActionResult Login(Customer obj){Session.Clear(); },此會話的類型爲HttpSessionStateBase –
var result =(RedirectToRouteResult)controller.Login(new Customer(){AdminLogin =「admin」,Password =「admin」}); –