visual-studio 运行 Visual Studio ASP.NET 单元测试时出现 500 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/260432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
500 Error Running Visual Studio ASP.NET Unit Test
提问by marc
I have the following method in my unit test project:
我的单元测试项目中有以下方法:
[TestMethod]
[HostType("ASP.NET")]
[UrlToTest("http://localhost:3418/Web/SysCoord/ChooseEPA.aspx")]
[AspNetDevelopmentServerHost("%PathToWebRoot%")]
public void TestMethod1()
{
Page page = TestContext.RequestedPage;
Assert.IsTrue(false, "Test ran, at least.");
}
I'm getting this exception:
我收到此异常:
The test adapter 'WebHostAdapter' threw an exception while running test 'TestMethod1'. The web site could not be configured correctly; getting ASP.NET process information failed. Requesting 'http://localhost:3418/SysCoord/VSEnterpriseHelper.axd' returned an error: The remote server returned an error: (404) Not Found. The remote server returned an error: (404) Not Found.
测试适配器“WebHostAdapter”在运行测试“TestMethod1”时抛出异常。无法正确配置网站;获取 ASP.NET 进程信息失败。请求“ http://localhost:3418/SysCoord/VSEnterpriseHelper.axd”返回错误:远程服务器返回错误:(404)未找到。远程服务器返回错误:(404) 未找到。
The page works as it should in a browser at the url: http://localhost:3418/Web/SysCoord/ChooseEPA.aspx.
该页面在浏览器中的 url 中正常工作:http://localhost:3418/Web/SysCoord/ChooseEPA.aspx。
This physical path is: C:\ESI\HR_Connect2\BenefitChangeSystem\Application_DEV\Web\SysCoord.
此物理路径为:C:\ESI\HR_Connect2\BenefitChangeSystem\Application_DEV\Web\SysCoord。
Any ideas would be appreciated.
任何想法,将不胜感激。
Update 1
更新 1
Added the following to my web.config file per this article. Also made the web.config writable and killed/restarted the development web server. No change in behavior.
根据本文将以下内容添加到我的 web.config 文件中。还使 web.config 可写并终止/重新启动开发 Web 服务器。行为没有变化。
<location path="VSEnterpriseHelper.axd">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
Update 2
更新 2
Changing the AspNetDevelopmentServerHost attribute to the equivalent of [AspNetDevelopmentServerHost("%PathToWebRoot%\solutionfolder\webfolder", "/webfolder")] resolved the 404 problem.
将 AspNetDevelopmentServerHost 属性更改为等效于 [AspNetDevelopmentServerHost("%PathToWebRoot%\solutionfolder\webfolder", "/webfolder")] 解决了 404 问题。
Unfortunately the test began to return a 500 error instead. Progress, but not much. Trial and error with a clean project led to the conclusion that references to custom classes in the of the web.config were causing the problem.
不幸的是,测试开始返回 500 错误。进步,但不多。对干净项目的反复试验得出的结论是,对 web.config 中的自定义类的引用导致了问题。
For example:
例如:
<profile enabled="true" defaultProvider="MyProfileProvider">
<providers>
<add name="MyProfileProvider" connectionStringName="ProfileConnectionString" applicationName="/MyApp" type="System.Web.Profile.SqlProfileProvider"/>
</providers>
<properties>
<add name="Theme" type="String" defaultValue="Default"/>
<add name="LastLogon" type="DateTime"/>
<add name="LastLogonIp" type="String"/>
<!--
<add name="EmployeeSearchCriteria" type="MyApplicationFramework.Profile.EmployeeSearchCriteria"/>
<add name="DocumentSearchCriteria" type="MyApplicationFramework.Profile.DocumentSearchCriteria"/>
-->
</properties>
</profile>
With the criteria types above commented out the test ran fine. With them uncommented, the 500 error was returned.
注释掉上面的标准类型后,测试运行良好。取消注释后,返回 500 错误。
Anyone had a similar problem in the past?
过去有人遇到过类似的问题吗?
回答by Igor Zevaka
I've had this problem before and at that point gave up after reading all I could google about it (including this thread).
我之前遇到过这个问题,在阅读了我可以用谷歌搜索的所有内容后放弃了(包括这个线程)。
The solution turned out to be simple in my case. All I had to do was not use ASP.NET test attributes and simply test the MVC project as a DLL.
就我而言,解决方案很简单。我所要做的就是不使用 ASP.NET 测试属性,而只是将 MVC 项目作为 DLL 进行测试。
Step 1
步骤1
Remove the extra attributes from the test.
从测试中删除额外的属性。
[TestMethod]
public void TestMethod1()
{
Page page = TestContext.RequestedPage;
Assert.IsTrue(false, "Test ran, at least.");
}
Step 2
第2步
In Code Coverage, uncheck the MVC Project and add the MVC Project's DLL manually.
在代码覆盖率中,取消选中 MVC 项目并手动添加 MVC 项目的 DLL。


Voila, it get instrumented as a normal assembly, no errors, doesn't spin up the Development Server, also doesn't fail the Team Build.
瞧,它作为正常程序集进行检测,没有错误,不会启动开发服务器,也不会使团队构建失败。
回答by hal9000
I found that using vs2010 I am not restricted to just 4.0 applications. I DID however find that if testing a web application and you are using the old System.Web.Extensions version redirect you may get an error. Removing the following section from the Web.configfile fixed my issue:
我发现使用 vs2010 我不仅限于 4.0 应用程序。然而,我发现如果测试 Web 应用程序并且您使用的是旧的 System.Web.Extensions 版本重定向,您可能会收到错误消息。从Web.config文件中删除以下部分修复了我的问题:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1" appliesTo="v2.0.50727">
<dependentAssembly>
<assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
Good luck.
祝你好运。
回答by ben
I was getting the same problem as you, however my experience was a little different.
我遇到了和你一样的问题,但是我的经历有点不同。
I am on vista x64, my developers are in xp x64...they haven't been having any issues at all. I just upgraded and could not run any unit test for a asp.net MVC project. I was receiving the same 500 error you were receiving.
我在 vista x64 上,我的开发人员在 xp x64 上……他们根本没有遇到任何问题。我刚刚升级,无法为 asp.net MVC 项目运行任何单元测试。我收到了与您收到的相同的 500 错误。
I turned off code coverage, everything magically started working.
我关闭了代码覆盖率,一切都神奇地开始工作了。
回答by cfeduke
Based on your evidence I would guess that a reference to whichever assembly contains MyApplicationFramework.Profile.EmployeeSearchCriteriais missing from either the unit test project or the web project - though I would really think that you would only require the reference in the web project but I'm not knowledgeable about how the VS web server behaves when used as part of a unit test.
根据您的证据,我猜测MyApplicationFramework.Profile.EmployeeSearchCriteria单元测试项目或 Web 项目中缺少对任何程序集的引用- 尽管我真的认为您只需要 Web 项目中的引用,但我不了解VS Web 服务器在用作单元测试的一部分时的行为方式。
回答by Ibsta
I got the same error message while unit testing a web app with Visual Studio 2010. The only difference is that i was using IIS, ie i ommited the [AspNetDevelopmentServerHost("%PathToWebRoot%")]directive.
在使用 Visual Studio 2010 对 Web 应用程序进行单元测试时,我收到了相同的错误消息。唯一的区别是我使用的是 IIS,即我省略了该 [AspNetDevelopmentServerHost("%PathToWebRoot%")]指令。
I suspect the problem lies in the fact that i was using IIS version 5.1. More here:
我怀疑问题在于我使用的是 IIS 5.1 版。更多在这里:
http://ibsta.blogspot.com/2011/01/unit-testing-fun-under-visual-studio.html
http://ibsta.blogspot.com/2011/01/unit-testing-fun-under-visual-studio.html
回答by simpsons88
I setup a default unit test which popped up with the error, that brought me here. I just removed the following (below). Then clicked debug current context and boom, fine :S.
我设置了一个默认的单元测试,它弹出了错误,把我带到了这里。我刚刚删除了以下内容(如下)。然后单击调试当前上下文和繁荣,很好:S。
[HostType("ASP.NET")]
[AspNetDevelopmentServerHost("C:\Inetpub\....]
回答by Bigdog
I ran into a similar issue testing a webservice where the project is .NET 3.51. I was getting a IIS 500 error. I removed the old assembly bindinds as commented by Hal Diggs and it worked.
我在测试项目为 .NET 3.51 的网络服务时遇到了类似的问题。我收到 IIS 500 错误。我删除了 Hal Diggs 评论的旧程序集 bindinds 并且它起作用了。
回答by Rashedul.Rubel
I experienced the same problem. Then I checked the properties of both the web project and the unit test project. And found that target framework was set different from each other. I set the target framework of both the project to .Net framework 4 (in my case). Finally ran the test method again and it worked.
我遇到了同样的问题。然后我检查了 web 项目和单元测试项目的属性。并发现目标框架设置不同。我将两个项目的目标框架都设置为 .Net framework 4(就我而言)。最后再次运行测试方法,它起作用了。
Thanks.
谢谢。
回答by Mark
For me it was Resharper that caused this problem. Once I suspended it (Tools -> Options -> Resharper -> General -> Suspend) everything worked. (using VS2010SP1 and Resharper Ultimate 2016.1.2)
对我来说,是 Resharper 导致了这个问题。一旦我暂停它(工具 -> 选项 -> Resharper -> 常规 -> 暂停)一切正常。(使用 VS2010SP1 和 Resharper Ultimate 2016.1.2)
回答by Mark
I have faced same problem with unit testing. And found the problem reason and also solve it. The problem is only with access rights for the directory.
我在单元测试中遇到了同样的问题。并找到了问题的原因并解决了它。问题仅在于目录的访问权限。
In my case I have installed VSTS on the different drive(d) then default. So only to give the full access rights to the user PCNAME\ASPNETfor whole directory \Program Files\Microsoft Visual Studio 9.0.
在我的情况下,我已经在不同的驱动器(d)上安装了 VSTS,然后是默认的。因此,仅授予用户PCNAME\ASPNET对整个目录\Program Files\Microsoft Visual Studio 9.0的完全访问权限。
I m using the windows XP but if u are using window server then give access rights to the user NetworkServices.
我使用的是 Windows XP,但如果您使用的是窗口服务器,则向用户NetworkServices授予访问权限。
By this solution i have solved my problem. Hope u find something useful from this Answer.
通过这个解决方案,我解决了我的问题。希望你从这个答案中找到有用的东西。
Thanks, Priyesh Patel
谢谢,普里耶什帕特尔

