views:

22

answers:

1

So the MvcContrib TestHelpers create mock versions of the following

  • HttpContext
  • HttpRequest
  • HttpResponse
  • HttpSession
  • Form
  • TempData
  • QueryString
  • ApplicationPath
  • PathInfo

within a fake controller when using this kind of code

var _controller = new FooController();        
var _builder = new TestControllerBuilder();
_builder.InitializeController(_controller);

But you'll notice they don't create a fake/mock Server object. Presumably there is a good reason why not. But I'm trying to stub out the Server.MapPath() method because the method in my SUT uses it and of course during the test its returning NULL.

The TestHelpers use rhino mocks (v3.5) and so am I. I know the syntax to stub out a method but how do I get the Server fake/mock object into my controller so I can stub out the method?

A: 

The HttpServerUtility class is sealed, but Microsoft provides an abstract HttpServerUtilityBase which can be used for mocking (and which MVCContrib uses). Simon's problem is due to a bug in MVCContrib. See: http://stackoverflow.com/questions/3248270/trying-to-stub-server-mappath-with-mvccontrib-test-helpers-and-rhino-mocks-3-5 for a solution.

Patrick Steele