ASP.NET应用程序的单元测试配置 [英] Unit Test configuration for ASP.NET application

查看:106
本文介绍了ASP.NET应用程序的单元测试配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我对Asp.Net Web应用程序的第一次测试. 我们有一个由几个模块组成的引擎.我需要测试引擎模块中的类. 尽管这些小类是Asp.Net App的一部分,但它们仅由业务逻辑组成.

This is my first test for Asp.Net Web Application. We have an Engine consisting of several modules. I need to test classes in Engine Module. Though these clases are part of Asp.Net App, they consists of only business logic.

除了作为WebApp的一部分之外,我如何孤立地测试这些类?因为我遇到了这个错误

How can I test these classes in isolation other being part of WebApp ? because i am getting this error

Web请求" http://localhost:8936/"已成功完成,而没有运行测试.当配置Web应用程序进行测试失败时(处理请求时会出现ASP.NET服务器错误),或者没有执行ASP.NET页时(URL可能指向HTML页,Web服务或目录列表).在ASP.NET中运行测试需要URL解析为ASP.NET页,并且该页才能在Load事件之前正常执行.来自请求的响应与测试结果一起存储在文件"WebRequestResponse_BlogManagerBPOConstr.html"中;通常,可以使用Web浏览器打开该文件以查看其内容.

The Web request 'http://localhost:8936/' completed successfully without running the test. This can occur when configuring the Web application for testing fails (an ASP.NET server error occurs when processing the request), or when no ASP.NET page is executed (the URL may point to an HTML page, a Web service, or a directory listing). Running tests in ASP.NET requires the URL to resolve to an ASP.NET page and for the page to execute properly up to the Load event. The response from the request is stored in the file 'WebRequestResponse_BlogManagerBPOConstr.html' with the test results; typically this file can be opened with a Web browser to view its contents.

谢谢

@Mark,这是设计人员生成的TestMethods之一.

@Mark, this is one of the TestMethods generated by designer

/

// <summary>
        ///A test for BlogManagerBPO Constructor
        ///</summary>
        // TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
        // http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,
        // whether you are testing a page, web service, or a WCF service.
        [TestMethod()]
        [HostType("ASP.NET")]
        [AspNetDevelopmentServerHost("D:\\WorkingCopies\\MyProject\\Engine", "/")]
        [UrlToTest("http://localhost:8936/")]
        public void BlogManagerBPOConstructorTest()
        {
            BlogManagerBPO target = new BlogManagerBPO();
            Assert.Inconclusive("TODO: Implement code to verify target");
        }

推荐答案

您收到的异常消息听起来根本就不是单元测试.您是要运行Visual Studio Web测试套件吗?

The exception message that you are getting doesn't sound like it's a unit test at all. Are you trying to run a Visual Studio Web Test suite instead?

对于单元测试,您应该只需能够创建业务逻辑类的实例并对其进行测试,而不会受到ASP.NET运行时的干扰.

For unit testing, you should simply be able to create instance of the business logic classes and test them without interference from the ASP.NET runtime.

在MsTest中,可能看起来像这样:

In MsTest, that might look like this:

[TestMethod]
public void Test5()
{
    var sut = new Thing();
    var expectedResult = new object();
    sut.Bar = expectedResult;
    var actual = sut.Bar;
    Assert.AreEqual(expectedResult, actual);
}

(虽然这可能不是最令人兴奋的测试……)

(perhaps not the most exciting test, though...)

在任何地方都找不到ASP.NET细节.

No ASP.NET specifics anywhere to be found.

如果将业务逻辑放在单独的库中并确保它不引用System.Web等,则最好确保这一点.

This is best ensured if you place the business logic in a seperate library and ensure that it doesn't reference System.Web, etc.

这篇关于ASP.NET应用程序的单元测试配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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