【问题标题】:Unit Test Web Forms on IIS and accessing controls on page Visual Studio 2012IIS 上的单元测试 Web 窗体和访问 Visual Studio 2012 页面上的控件
【发布时间】:2014-07-18 15:42:01
【问题描述】:

我正在尝试对包含逻辑和页面访问控制的方法背后的代码进行单元测试。我看过很多例子,例如http://kmanojt.blogspot.com/2010/09/unit-testing-aspx-and-acsx-pages-with.html 或http://www.infosysblogs.com/microsoft/2008/01/aspnet_unit_testing_on_iis.html。

如果我添加 HostType,那么我将无法再调试任何东西。如果我删除 HostType 并只留下 UrlToTest 我可以调试,但 testContextInstance.RequestedPage 始终为空。因为页面为空,我无法访问页面上的任何控件,并且当它调用 Method1 时,它会引发异常,因为它试图访问的文本框控件为空。我使用的是 IIS 7,所以没有使用 AspNetDevelopmentServerHost 设置。

示例代码:

使用 Microsoft.VisualStudio.TestTools.UnitTesting.Web;

使用 Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass()]

公共类TestClass {

private TestContext testContextInstance;

public TestContext TestContext {
    get { return testContextInstance; }
    set { testContextInstance = value; }
}

[TestMethod(), HostType("ASP.NET"), UrlToTest("https://localhost/xxx/Home.aspx")]
public void TestMethod()
{
    System.Web.UI.Page page = testContextInstance.RequestedPage;

    PrivateObject po = new PrivateObject(page);

    po.Invoke("Method1");
}

}

【问题讨论】:

    标签: asp.net unit-testing iis-7 webforms


    【解决方案1】:

    您需要附加到 W3WP.exe 或执行 Debugger.Break()。有时您可能需要在不同的 Visual Studio 实例中进行调试。请参阅以下链接。 http://forums.asp.net/t/1246417.aspx?How+to+Debug+unit+tests+for+ASP+NET+web+application

    【讨论】:

      猜你喜欢
      • 2010-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-16
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 2014-09-07
      相关资源
      最近更新 更多