.NET how to wash an HttpContext object

for unit test I'm trying to add a value to an HttpApplicationState object, which is an Application property of the HttpContext.Current class. I try with the 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. Values ​​do not get

What am I doing wrong?

+3
source share
1 answer

Depending on the version of .NET you are aiming for, you might have a look at HttpContextBase and HttpContextWrapper . HttpContextBase is abstract, so mocking frameworks like moq will let you assign its properties anyway.

0
source

Source: https://habr.com/ru/post/1775629/


All Articles