首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在单元测试中设置HttpContext.Current.Session

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

Stack Overflow用户
提问于 2012-03-09 04:15:31
回答 12查看 184.7K关注 0票数 196

我有一个正在尝试进行单元测试的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为空。

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

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9624242

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档