How to enable HttpContext in Unit Tests
Uncategorized December 22nd, 2008When you are using Cache in your Business Logic, then running tests will fail because there will be no HttpContext available. This is a workaround:
Add this to your App.Config:
1 2 3 | <appSettings> <add key="AppPhysicalDir" value="..."/> </appSettings> |
Then add these lines of code to your “public static void MyClassInitialize(TestContext testContext)”:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | using System.Configuration; using System.Web.Hosting; using System.Web; ... string appVirtualDir = "/"; string appPhysicalDir = ConfigurationManager.AppSettings["AppPhysicalDir"]; string page = "/default.aspx"; string query = string.Empty; TextWriter output = null; SimpleWorkerRequest workerRequest = new SimpleWorkerRequest(appVirtualDir, appPhysicalDir, page, query, output); HttpContext.Current = new HttpContext(workerRequest); |
Are there other ways of doing this?
No TweetBacks yet. (Be the first to Tweet this post)
Follow Me!