测试包含HttpContext.Current的代码

虽说直接测试这样的代码有违设计的原则,但用下面的方法可以对已有这样的代码进行单元测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[TestMethod]
public  void  Test_CreateHttpSessionTest()
{
     HttpContext.Current = new  HttpContext( new  HttpRequest( string .Empty, "http://localhost/" , string .Empty),
                                           new  HttpResponse( new  StringWriter()));
 
     SessionStateItemCollection sessions = new  SessionStateItemCollection();
 
     sessions[ "PartnerAlias" ] = "devtest1" ;
 
     IHttpSessionState iSessState = new  HttpSessionStateContainer
                                         ( string .Empty,sessions,
                                          new  HttpStaticObjectsCollection(),20000, true ,
                                          HttpCookieMode.UseCookies,SessionStateMode.Off, false );
 
     SessionStateUtility.AddHttpSessionStateToContext(HttpContext.Current,iSessState);
 
     Assert.IsNotNull(HttpContext.Current.Session);
 
     Assert.AreEqual(HttpContext.Current.Session[ "partnerAlias" ].ToString(), "devtest1" );
     Assert.IsNotNull(HttpContext.Current.Cache);
}
本文转自敏捷的水博客园博客,原文链接http://www.cnblogs.com/cnblogsfans/archive/2009/09/18/1569108.html如需转载请自行联系原作者

王德水

你可能感兴趣的:(测试)