开发者

.NET how to fudge the HttpContext object

for a unit test I am trying to add a value to the HttpApplicationState object, which is the Application property of the HttpContext.Current class. I try with th开发者_开发百科e following code

        TextWriter tw = new StringWriter();
        HttpWorkerRequest wr = new SimpleWorkerRequest("/webapp", @"path...", "logon.asp", "", tw);

        HttpContext.Current = new HttpContext(wr);

        //I try the following 2 lines
        HttpContext.Current.Application["KeyValue"] = "myValue";
        HttpContext.Current.Application.Add("KeyValue", "myValue");

        var count = HttpContext.Current.Application.Count;
        var get1 = HttpContext.Current.Application["KeyValue"];
        var get2 = HttpContext.Current.Application.Get("KeyValue");

But HttpContext.Current.Application.Count is always zero. The values do not get

What am I doing wrong?


Depending on the version of .NET you're targeting you might want to look into HttpContextBase and HttpContextWrapper. HttpContextBase is abstract so mocking frameworks like moq will allow you to assign its properties anyway you choose.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜