相关疑难解决方法(0)

在单元测试中设置HttpContext.Current.Session

我有一个Web服务,我正在尝试进行单元测试.在服务中,它从HttpContext类似物中提取了几个值,因此:

 m_password = (string)HttpContext.Current.Session["CustomerId"];
 m_userID = (string)HttpContext.Current.Session["CustomerUrl"];
Run Code Online (Sandbox Code Playgroud)

在单元测试中,我使用简单的工作者请求创建上下文,如下所示:

SimpleWorkerRequest request = new SimpleWorkerRequest("", "", "", null, new StringWriter());
HttpContext context = new HttpContext(request);
HttpContext.Current = context;
Run Code Online (Sandbox Code Playgroud)

但是,每当我尝试设置值时 HttpContext.Current.Session

HttpContext.Current.Session["CustomerId"] = "customer1";
HttpContext.Current.Session["CustomerUrl"] = "customer1Url";
Run Code Online (Sandbox Code Playgroud)

我得到null引用异常,表示HttpContext.Current.Session为null.

有没有办法在单元测试中初始化当前会话?

c# unit-testing web-services httpcontext

180
推荐指数
8
解决办法
15万
查看次数

标签 统计

c# ×1

httpcontext ×1

unit-testing ×1

web-services ×1