如何使用VS2008对经典ASP.NET项目中的方法运行单元测试?

时间:2021-12-22 01:39:47

I just wrote some new utility methods within a non-page class for an existing .NET webforms application and decided to try to unit test using the VS unit test framework rather than testing by hand. I've used the unit test tools before, but only on EXE and MVC projects, and am encountering an error connecting to the WebHostAdapter no matter what I've tried so far.

我刚刚在非页面类中为现有的.NET webforms应用程序编写了一些新的实用程序方法,并决定尝试使用VS单元测试框架进行单元测试而不是手动测试。我之前使用过单元测试工具,但仅限于EXE和MVC项目,并且无论我到目前为止尝试过什么,都遇到连接到WebHostAdapter的错误。

The current test:

目前的测试:

    [TestMethod()]
    [HostType("ASP.NET")]
    [AspNetDevelopmentServerHost("C:\\inetpub\\wwwroot\\sitepath", "/")]
    [UrlToTest("localhost/Login.aspx")]
    public void UrlFriendlyTitleTest()
    {
        string dirtyPath = "São\\Hell & High water";
        string expected = "sao/hell-high-water";
        string actual;
        actual = GeneralUtility_Accessor.UrlFriendlyTitle(dirtyPath);
        Assert.AreEqual(expected, actual);
        Assert.Inconclusive("Verify the correctness of this test method.");
    }

The error I'm encountering:

我遇到的错误:

The test adapter 'WebHostAdapter' threw an exception while running test 'UrlFriendlyTitleTest'. The web site could not be configured correctly; getting ASP.NET process information failed. Requesting 'localhost:53874/VSEnterpriseHelper.axd' returned an error: The remote server returned an error: (500) Internal Server Error. The remote server returned an error: (500) Internal Server Error.

测试适配器'WebHostAdapter'在运行测试'UrlFriendlyTitleTest'时抛出异常。无法正确配置网站;获取ASP.NET进程信息失败。请求'localhost:53874 / VSEnterpriseHelper.axd'返回错误:远程服务器返回错误:(500)内部服务器错误。远程服务器返回错误:(500)内部服务器错误。

What I've tried so far:

到目前为止我尝试了什么:

Set UrlToTest to current running debug path for a page in the project.
Promoted application to the local default site (reflected in current value of UrlToTest).
Set UrlToTest to point to an unauthenticated page (it shouldn't matter which page is called--I'm testing a method that's not even referenced yet).
Suggestions from MSDN boards (unable to post more than 1 link).
And I've tried the suggestions and web.config changes listed by the author of this SO question.

将UrlToTest设置为项目中页面的当前运行调试路径。将应用程序提升到本地默认站点(反映在UrlToTest的当前值中)。将UrlToTest设置为指向未经身份验证的页面(调用哪个页面无关紧要 - 我正在测试一个甚至尚未引用的方法)。来自MSDN主板的建议(无法发布超过1个链接)。我已经尝试了这个SO问题的作者列出的建议和web.config更改。

Is there a canonical answer for this issue that I've missed?

我错过了这个问题的规范答案吗?

1 个解决方案

#1


Typically, I'll have all of my non-page classes defined in a separate assembly (class library) that gets referenced from the web site. It's much easier to unit test class libraries than it is to test web sites. My advice would be to move your GeneralUtility class into a class library and test it from there.

通常,我将在一个单独的程序集(类库)中定义所有非页面类,并从Web站点引用。单元测试类库比测试网站要容易得多。我的建议是将您的GeneralUtility类移动到类库中并从那里进行测试。

#1


Typically, I'll have all of my non-page classes defined in a separate assembly (class library) that gets referenced from the web site. It's much easier to unit test class libraries than it is to test web sites. My advice would be to move your GeneralUtility class into a class library and test it from there.

通常,我将在一个单独的程序集(类库)中定义所有非页面类,并从Web站点引用。单元测试类库比测试网站要容易得多。我的建议是将您的GeneralUtility类移动到类库中并从那里进行测试。