UPDATE #1:
While my original answer may be useful when writing tests that use certain aspects of HttpContext
it is not that easy to use it with the HttpContext.Application
property as that property relies on a static internal
property of the HttpApplicationFactory
class.
A possible solution would be not to rely on the Application
property in the method under test, but instead pass the value into the method. For example:
cls.MethodUnderTest("foo value");
This then means you can change your test class so it has no reliance on HttpContext.Application
(assuming no other tests need it) and your test class will simply contain the test:
[TestMethod]
public void Test1()
{
// This would be the value you previously stored in the Application property
object fooValue = <some value>;
YourClassName cls = new YourClassName();
cls.MethodUnderTest(fooValue);
}
Then if you have other tests that test the method with different values you can simply create a test and pass in that value rather than having to add to add it to HttpContext.Application
first.
As a side note....while typing this I have realised that I could probably do the same thing with my unit tests and cookies!
Original Answer
I am currently attempting a similar thing, but with cookies. What I do is pass HttpContext.Current
into the class, store a reference, and then use that within the class methods.
So:
public class MyClass
{
private HttpContext _httpContext;
// **** The caller would supply HttpContext.Current here ****
public MyClass(HttpContext httpContext) {
_httpContext = httpContext;
}
public void SomeMethod() {
...
// Do something here with _httpContext
...
}
}
Then when it comes to unit testing I would do something similar to what you have at the moment like this:
public void Test1()
{
HttpRequest request = new HttpRequest(null, "http://tempuri.org", null);
HttpResponse response = new HttpResponse(null);
HttpContext httpContext = new HttpContext(request, response);
httpContext.Request.Cookies.Add(new HttpCookie("cookieName"));
MyClass cls = new MyClass(httpContext);
cls.SomeMethod();
}
For your situation you could change your class that references HttpContext.Current
to accept a HttpContext
instance and pass HttpContext.Current
into it like the example above. Then, instead of setting up HttpContext.Current
in TestInitialize
you would create a member variable and use that instead of relying of the static Current
property.
For example:
private const string Foo = "foobar";
private HttpContext _httpContext = null;
[TestInitialize]
public void InitializeContext()
{
_httpContext = new HttpContext(new HttpRequest(
null, "http://tempuri.org", null), new HttpResponse(null));
_httpContext.Application["fooKey"] = Foo;
}
[TestMethod]
public void Test1() <-- **** Example Test Method ****
{
YourClassName cls = new YourClassName(_httpContext);
cls.MethodUnderTest();
}
[TestCleanup]
public void DisposeContext()
{
_httpContext = null;
}
I don't have a working example to hand, so I'm just going off memory at the moment, but hopefully it will provide some help.