在IIS上对Web表单进行单元测试,并访问Visual Studio 2012页面上的控件



我正试图对具有逻辑的方法和页面上的访问控制后面的代码进行单元测试。我已经看了很多例子,例如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总是null。因为页面为空,我不能访问页面上的任何控件,当它调用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");
}

}

您需要附加到W3WP.exe或执行debuger . break()。有时您可能需要在不同的Visual Studio实例中进行调试。请看下面的链接。http://forums.asp.net/t/1246417.aspx?How + +调试+单位+测试+ + ASP +网络+网络+应用

最新更新