今天花了几个小时试图针对一个ASP.NET项目编写一些单元测试。这是2010。
在IIS7中使用Windows7Enterprise。
我采取的步骤是:
在我的Windows和IIS6计算机上执行这些精确的步骤时,它工作得很好。
然而,在 IIS7上的Windows 7企业机器上,我得到:
指定的URL ('http://localhost/MyProject')不对应于有效目录。在IIS中配置为在ASP.NET中运行的测试需要为该URL存在一个有效目录。URL可能无效,也可能没有指向有效的Web应用程序。
所以发生了什么,我可以确认我可以浏览到http://localhost/MyProject,它显示得很完美。
我确信我错过了Windows/IIS中的某种配置,但我真的很茫然。
生成的测试方法:
<TestMethod(), _
HostType("ASP.NET"), _
UrlToTest("http://localhost/MyProject")> _
Public Sub MyMethodTest()
Dim target As Member_Accessor = New Member_Accessor() ' TODO: Initialize to an appropriate value
Dim CurrentVal As Short = 0 ' TODO: Initialize to an appropriate value
Dim expected As Short = 0 ' TODO: Initialize to an appropriate value
Dim actual As Short
actual = target.MyMethod(CurrentVal)
Assert.AreEqual(expected, actual)
Assert.Inconclusive("Verify the correctness of this test method.")
End Sub(交叉张贴在ASP.NET论坛)
发布于 2012-02-07 02:24:28
这可能是权限问题。
如果您使用的是默认目录(C:\users\Documents\Visual 2010\Projects),则应用标识池在那里没有权限。您必须在类似于C:\webs的地方创建一个项目,并确保应用程序池标识具有文件夹的权限。
请参阅Rick在http://blogs.msdn.com/b/rickandy/archive/2011/04/22/test-you-asp-net-mvc-or-webforms-application-on-iis-7-in-30-seconds.aspx的博客文章,看看这是否有帮助。
发布于 2012-02-03 08:40:35
如果您以前没有做过单元测试,我建议您首先尽可能干净地测试类的功能。尝试将您的功能分解为可以单独测试而不依赖于web上下文的小部分。
看看这个问题,关于什么是单元测试?的一个概念,这里是一个MSDN杂志关于测试的文章,您也可以看看这个博客。示例使用的是NUnit,但如果使用MSTest,则主体是相同的。
我还可以推荐Roy图书单元测试技术
在这种情况下,如果成员类不依赖于web上下文,则不需要IIS,而只需执行以下操作:
<TestMethod()> _
Public Sub MyMethodTest()
Dim target = New Member()
Dim CurrentVal As Short = 0 ' TODO: Initialize to an appropriate value
Dim expected As Short = 0 ' TODO: Initialize to an appropriate value
Dim actual As Short
actual = member.MyMethod(CurrentVal)
Assert.AreEqual(expected, actual)
End Sub发布于 2012-05-06 03:41:24
我今天也遇到了同样的问题。经过一些研究后,我发现了这条线,它建议我检查我的事件日志。在这样做时,我发现了许多类似于以下内容的错误:
(QTAgent32.exe, PID 12348, Thread 61) WebSites.GetWebServer: failed to
create AspNetHelper:
Microsoft.VisualStudio.Enterprise.Common.AspNetHelperException: The
website metabase contains unexpected information or you do not have
permission to access the metabase. You must be a member of the
Administrators group on the local computer to access the IIS metabase.
Therefore, you cannot create or open a local IIS Web site. If you
have Read, Write, and Modify Permissions for the folder where the
files are located, you can create a file system web site that points
to the folder in order to proceed. --->
System.Runtime.InteropServices.COMException: Unknown error
(0x80005000)这让我想到了这篇博客文章,它似乎解决了这个问题。
我只需要“打开或关闭Windows功能”,并添加IIS 6管理兼容性和所有四个子组件。我正在运行Windows 7家庭高级版,它没有Windows身份验证选项,但这似乎不是一个问题。试一试,看看这是否解决了你的问题。
https://stackoverflow.com/questions/9065937
复制相似问题