Mocking HttpContext.GetGlobalResourceObject in NUnit with Moq

匆匆过客 提交于 2019-12-05 18:44:38
  1. Have your class under test take a dependency on HttpContextBase (which is abstract).

  2. In normal execution use an HttpContextWrapper (which derives from HttpContextBase) to wrap the HttpContext and supply that to your class.

  3. Then in your unit tests you can pass in a mock HttpContextBase whose GetGlobalResourceObject you control.

It is simple but it requires you to rethink how you design parts of your application. This kind of inversion of control is essential to writing code that's easy to unit test. It may help to use an IoC container.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!