tags:

views:

16

answers:

1

WCF project in a Test harness.

Why would HttpContext.Current.Server.MapPath("~/"); or HttpContext.Current.Server.MapPath(".");

fail in my test?

I am attempting to put together a PDF building module for existing app. I have to identify where to write the final output.

TIA

+2  A: 

The reason your unit tests are failing is because HttpContext.Current requires an ASP.NET context which is close to impossible to recreate in a unit test. Frameworks such as ASP.NET MVC abstract away this using HttpContextBase which can be easily mocked in an unit test.

Also you shouldn't be using HttpContext in WCF. If you self host your WCF service it will blow not only in the unit tests but at execution which is even worse.

Conclusion:

  • Every time you write HttpContext.Current in a method by definition this method is not unit testable so don't even try to test it.
  • Never use HttpContext.Current in WCF (and if you read the previous point, not only in WCF :-))
Darin Dimitrov
+1 Amen, brother! :-)
marc_s
Will use relativePath = AppDomain.CurrentDomain.BaseDirectory; Thanks
SteveO