This is what I used for Unit Test friendly session caching. By checking HttpContext.Current for null, you pass caching for nunit tests and still allow your program to function normally.
This is the simplest solution without making a lot of code changes to your project.
internal class SessionCache { public static void Store(string key, object val) { if (HttpContext.Current != null) { HttpContext.Current.Session[key] = val; } } public static object Retrieve(string key) { if (HttpContext.Current != null) { return HttpContext.Current.Session[key]; } return null; } }
source share