运行Visual Studio ASP.NET单元测试时出错500错误

mar*_*arc 16 unit-testing visual-studio-2008 visual-studio

我的单元测试项目中有以下方法:

    [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.");
    }
Run Code Online (Sandbox Code Playgroud)

我得到了这个例外:

测试适配器'WebHostAdapter'在运行测试'TestMethod1'时引发异常.无法正确配置网站; 获取ASP.NET进程信息失败.请求' http:// localhost:3418/SysCoord/VSEnterpriseHelper.axd '返回错误:远程服务器返回错误:(404)Not Found.远程服务器返回错误:(404)Not Found.

该页面在URL的浏览器中正常工作:http:// localhost:3418/Web/SysCoord/ChooseEPA.aspx.

此物理路径为:C:\ ESI\HR_Connect2\BenefitChangeSystem\Application_DEV\Web\SysCoord.

任何想法,将不胜感激.

更新1

根据本文,我的web.config文件中添加了以下内容.还使web.config可写并杀死/重新启动开发Web服务器.行为没有变化.

<location path="VSEnterpriseHelper.axd">
    <system.web>
        <authorization>
            <allow users="*"/>
        </authorization>
    </system.web>
</location>
Run Code Online (Sandbox Code Playgroud)

更新2

将AspNetDevelopmentServerHost属性更改为等效的[AspNetDevelopmentServerHost("%PathToWebRoot%\ solutionfolder\webfolder","/ webfolder")]解决了404问题.

不幸的是,测试开始返回500错误.进步,但并不多.使用干净的项目进行试验和错误得出的结论是,对web.config中的自定义类的引用导致了问题.

例如:

    <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>
Run Code Online (Sandbox Code Playgroud)

上面的标准类型被注释掉,测试运行良好.如果取消注释,则返回500错误.

过去任何人都有过类似的问题吗?

Igo*_*aka 15

我以前遇到过这个问题,并且在阅读了所有关于它的所有内容之后放弃了这个问题(包括这个帖子).

在我的案例中,解决方案变得简单.我所要做的就是不使用ASP.NET测试属性,只是将MVC项目作为DLL进行测试.

步骤1

从测试中删除额外的属性.

[TestMethod]
public void TestMethod1()
{
    Page page = TestContext.RequestedPage;
    Assert.IsTrue(false, "Test ran, at least.");
}
Run Code Online (Sandbox Code Playgroud)

第2步

在代码覆盖率中,取消选中MVC项目并手动添加MVC项目的DLL.

替代文字

瞧,它可以作为正常的程序集进行检测,没有错误,不会启动开发服务器,也不会使Team Build失败.


hal*_*000 8

我发现使用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>
Run Code Online (Sandbox Code Playgroud)

祝好运.


cfe*_*uke 1

根据您的证据,我猜测MyApplicationFramework.Profile.EmployeeSearchCriteria单元测试项目或 Web 项目中缺少对包含的程序集的引用 - 尽管我真的认为您只需要 Web 项目中的引用,但我不了解VS Web 服务器用作单元测试的一部分时的行为方式。