开发者

WCF location seen in a test

WCF project in a Test harness.

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

fail in my test?

I am attem开发者_StackOverflow社区pting to put together a PDF building module for existing app. I have to identify where to write the final output.

TIA


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 :-))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜