我有一个正在尝试进行单元测试的web服务。在服务中,它从HttpContext
中提取几个值,如下所示:
m_password = (string)HttpContext.Current.Session["CustomerId"];
m_userID = (string)HttpContext.Current.Session["CustomerUrl"];
在单元测试中,我使用一个简单的worker请求创建上下文,如下所示:
SimpleWorkerRequest request = new SimpleWorkerRequest("", "", "", null, new StringWriter());
HttpContext context = new HttpContext(request);
HttpContext.Current = context;
但是,每当我尝试设置HttpContext.Current.Session
的值时,
HttpContext.Current.Session["CustomerId"] = "customer1";
HttpContext.Current.Session["CustomerUrl"] = "customer1Url";
我得到了空引用异常,表明HttpContext.Current.Session
为空。
有没有办法在单元测试中初始化当前会话?
https://stackoverflow.com/questions/9624242
复制相似问题