【发布时间】: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