从单元测试项目为WebForms项目设置HttpApplicationState

时间:2019-04-03 15:09:25

标签: unit-testing testing webforms

我试图在WebForms ASP.net项目中的类中调用方法。我需要在“应用程序状态”中添加一个值,并且尝试了:

HttpContext.Current = new HttpContext(new HttpRequest(null, "http://tempuri.org", null), new HttpResponse(null));

var cfg = new MyConfiguration();
HttpContext.Current.Application.Lock();

// Neither of these adds the item to the collection.
HttpContext.Current.Application.Add("MyConfiguration", cfg);
HttpContext.Current.Application["MyConfiguration"] = cfg;

// Looking in the debugger, there are 0 keys in the Application collection.

HttpContext.Current.Application.UnLock();
cfg = (MyConfiguration)HttpContext.Current.Application["MyConfiguration"];
Assert.IsNotNull(cfg);

为什么这些项目不添加到ApplicationState集合中?

谢谢。

0 个答案:

没有答案