走进单元测试(3):消灭HttpContext的依赖,兼谈单元测试的设计辅助性

发表于:2010-6-04 10:46

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:gamix(cnblogs)    来源:51Testing软件测试网采编

分享:

  但对我们的需求来说,其实也非常的简单,既然他硬编码了,那我们再派生一下,把这些方法override一下不就可以了:

 1     /// <summary>
 2     /// Provides a simple implementation of the System.Web.HttpWorkerRequest abstract class that can be used to host ASP.NET applications outside an Internet Information Services (IIS) application.
 3     /// This class can be used for unit test which needs a web host.
 4     /// </summary>
 5     public class TestWorkerRequest : SimpleWorkerRequest
 6     {
 7         private readonly string hostName = "";
 8
 9         /// <summary>
10         /// Initializes a new instance of the <see cref="T:System.Web.Hosting.SimpleWorkerRequest"/> class when the target application domain has been created using the <see cref="M:System.Web.Hosting.ApplicationHost.CreateApplicationHost(System.Type,System.String,System.String)"/> method.
11         /// </summary>
12         /// <param name="page">The page to be requested (or the virtual path to the page, relative to the application directory).</param>
13         /// <param name="query">The text of the query string.</param>
14         /// <param name="output">A <see cref="T:System.IO.TextWriter"/> that captures output from the response</param>
15         /// <param name="hostName">The host name that will be requested.</param>
16         public TestWorkerRequest(string page, string query, TextWriter output, string hostName)
17             : base(page, query, output)
18         {
19             this.hostName = hostName;
20         }
21
22         /// <summary>
23         /// Initializes a new instance of the <see cref="T:System.Web.Hosting.SimpleWorkerRequest"/> class for use in an arbitrary application domain, when the user code creates an <see cref="T:System.Web.HttpContext"/> (passing the SimpleWorkerRequest as an argument to the HttpContext constructor).
24         /// </summary>
25         /// <param name="appVirtualDir">The virtual path to the application directory; for example, "/app".</param>
26         /// <param name="appPhysicalDir">The physical path to the application directory; for example, "c:\app".</param>
27         /// <param name="page">The virtual path for the request (relative to the application directory).</param>
28         /// <param name="query">The text of the query string.</param>
29         /// <param name="output">A <see cref="T:System.IO.TextWriter"/> that captures the output from the response.</param>
30         /// <param name="hostName">The host name that will be requested.</param>
31         /// <exception cref="T:System.Web.HttpException">The <paramref name="appVirtualDir"/> parameter cannot be overridden in this context.
32         /// </exception>
33         public TestWorkerRequest(string appVirtualDir, string appPhysicalDir, string page, string query, TextWriter output, string hostName)
34             : base(appVirtualDir, appPhysicalDir, page, query, output)
35         {
36             this.hostName = hostName;
37         }
38
39         /// <summary>
40         /// Returns the server IP address of the interface on which the request was received.
41         /// </summary>
42         /// <returns>
43         /// The server IP address of the interface on which the request was received.
44         /// </returns>
45         public override string GetLocalAddress()
46         {
47             return hostName;
48         }
49     }

32/3<123>
精选软件测试好文,快来阅读吧~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号