运行 Visual Studio ASP.NET 单元测试时出现 500 错误 [英] 500 Error Running Visual Studio ASP.NET Unit Test

查看:22
本文介绍了运行 Visual Studio ASP.NET 单元测试时出现 500 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.");
    }

我收到此异常:

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

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.

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

The page works as it should in a browser at the url: http://localhost:3418/Web/SysCoord/ChooseEPA.aspx.

该物理路径为:C:ESIHR_Connect2BenefitChangeSystemApplication_DEVWebSysCoord.

This physical path is: C:ESIHR_Connect2BenefitChangeSystemApplication_DEVWebSysCoord.

任何想法将不胜感激.

更新 1

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

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.

<location path="VSEnterpriseHelper.axd">
    <system.web>
        <authorization>
            <allow users="*"/>
        </authorization>
    </system.web>
</location>

更新 2

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

Changing the AspNetDevelopmentServerHost attribute to the equivalent of [AspNetDevelopmentServerHost("%PathToWebRoot%solutionfolderwebfolder", "/webfolder")] resolved the 404 problem.

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

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.

例如:

    <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>

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

With the criteria types above commented out the test ran fine. With them uncommented, the 500 error was returned.

过去有人遇到过类似的问题吗?

Anyone had a similar problem in the past?

推荐答案

我之前也遇到过这个问题,但在阅读了所有我能用谷歌搜索的内容(包括这个帖子)后放弃了.

I've had this problem before and at that point gave up after reading all I could google about it (including this thread).

在我的情况下,解决方案很简单.我所要做的就是不使用 ASP.NET 测试属性,而只是将 MVC 项目作为 DLL 进行测试.

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.

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

Remove the extra attributes from the test.

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

步骤 2

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

Step 2

In Code Coverage, uncheck the MVC Project and add the MVC Project's DLL manually.

瞧,它作为正常程序集进行检测,没有错误,不会启动开发服务器,也不会使团队构建失败.

Voila, it get instrumented as a normal assembly, no errors, doesn't spin up the Development Server, also doesn't fail the Team Build.

这篇关于运行 Visual Studio ASP.NET 单元测试时出现 500 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆