visual-studio-2008 - Visual Studio 单元测试中的 HttpRuntime

标签 visual-studio-2008 unit-testing

我可以在我的单元测试方法中访问 HttpRuntime 吗?当我每次尝试访问时,它都显示 HttpRuntime 在当前上下文中不存在。在我想要测试访问缓存变量的目标方法中


   HttpRuntime.Cache[key]; 

这可能吗?还是我在这里遗漏了什么?

谢谢

最佳答案

您可以使用 SimpleWorkerRequest 对象在单元测试中创建一个 HttpContext。

TextWriter writer = new StringWriter();
HttpWorkerRequest httpRequest = new SimpleWorkerRequest("virtualDir", "physicalDir", "page", "", writer);
HttpContext.Current = new HttpContext(httpRequest);
HttpContext.Current.Cache[key] = some value..

关于visual-studio-2008 - Visual Studio 单元测试中的 HttpRuntime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5025194/

相关文章:

javascript - 在 Jest 中模拟 Lambda 回调? (无法读取未定义的属性体)

unit-testing - 模拟对象的目的是什么?

java - Junit测试用例: Connection refused case

python - 如何在测试方法中模拟 protected /私有(private)方法?

visual-studio - 如何关闭 Visual Studio 的 "There is no source code available for the current location."?

c++ - 调试不在调试器内运行且没有崩溃的程序

c++ - 如何在卸载 DLL 或进程终止时释放资源

vb.net - 为什么我的数据库没有更新?

java - Mockito/PowerMocktio doNothing for none void 方法

c# - 如何列出网络上的所有打印机?