• 测试包含HttpContext.Current的代码


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

    [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);
    }
  • 相关阅读:
    nodeJs小练习之爬取北京昌平区房价
    2016,加油,奋斗
    1339 字符串王国的种族
    1333 明信片和照片
    1316 你能知道这是几进制数?
    1309 简化版九宫格
    1295 爱翻译
    1288 幸运转盘(一)
    1287 孙武练兵
    1284 喜羊羊的新年礼物
  • 原文地址:https://www.cnblogs.com/cnblogsfans/p/1569108.html
Copyright © 2020-2023  润新知