Can I access HttpRuntime
in my unit Test method. When I try to access every tim开发者_如何转开发e it shows HttpRuntime does not exist in the current context. In my target method which I do want to test accessing a Cache variable
HttpRuntime.Cache[key];
Is it possible ? or am I missing anything here?
Thanks
You can create an HttpContext inside your unit test with a SimpleWorkerRequest object.
TextWriter writer = new StringWriter(); HttpWorkerRequest httpRequest = new SimpleWorkerRequest("virtualDir", "physicalDir", "page", "", writer); HttpContext.Current = new HttpContext(httpRequest); HttpContext.Current.Cache[key] = some value..
Your best bet would be to refactor your target method to not access HttpRuntime directly, but instead to pass that information as a parameter or have it talk to an interface that wraps the HttpRuntime. That way you can break the dependency on HttpRuntime and make things much easier to test.
精彩评论