asp.net - 调用 VirtualPathUtility.ToAbsolute 的单元测试代码

标签 asp.net .net unit-testing

我正在尝试对调用 VirtualPathUtility.ToAbsolute 的一些代码进行单元测试.

VS 2008 提供的单元测试工具可以做到这一点吗?如果没有,是否可以使用更高版本的 Visual Studio?

最佳答案

我们已经过了 VS 2008,但对于仍在努力解决此问题的任何人,我已经在以下位置找到了解决方案:http://forums.asp.net/t/995143.aspx?Mocking+HTTPContext+object .

在您的测试初始化​​中使用以下代码来覆盖默认的 AppDomain 值。 (VirutalPathUtility 静态方法将使用您的新值。)

[TestInitialize]
public void Initialize()
{
    // Fake out env for VirtualPathUtility.ToAbsolute(..)
    string path = AppDomain.CurrentDomain.BaseDirectory; 
    const string virtualDir = "/";
    AppDomain.CurrentDomain.SetData(".appDomain", "*");
    AppDomain.CurrentDomain.SetData(".appPath", path);
    AppDomain.CurrentDomain.SetData(".appVPath", virtualDir);
    AppDomain.CurrentDomain.SetData(".hostingVirtualPath", virtualDir);
    AppDomain.CurrentDomain.SetData(".hostingInstallDir", HttpRuntime.AspInstallDirectory);
    TextWriter tw = new StringWriter();
    HttpWorkerRequest wr = new SimpleWorkerRequest("default.aspx", "", tw);
    HttpContext.Current = new HttpContext(wr);
}

关于asp.net - 调用 VirtualPathUtility.ToAbsolute 的单元测试代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/655134/

相关文章:

.net - SocketOptionName.ReuseAddress 有什么用?

c++ - 使用 Gtest 如何在 ON_CALL 中返回不同的值?

python - 在带有 unittest 的自动 Selenium 测试中使用多个 WebSocket 客户端

asp.net - Nodejs 与 SignalR : why do we need server-side javascript?

asp.net - 如何在asp.net和C#中实现登录 session

javascript - 如何在文件上传控件中选择默认文件夹位置?

c# - 确定系统架构

asp.net - 仅在特定域上加载 iframe

.net - 为什么 boolean 值比字符消耗更多的内存?

unit-testing - 运行单个Grails单元/集成测试将在Intellij上运行所有测试