asp.net-mvcunit-testingtddmvccontrib

Using / Mocking Request.Url when using MvcContrib TestHelper


I'm using MvcContrib.TestHelper, and initialize my controller like this:

var accountController = new AccountController();

var builder = new TestControllerBuilder();
builder.InitializeController(accountController);

My problem is, that inside my AccountController I have:

Request.Url.GetLeftPart(UriPartial.Authority);

However, this comes back as null. Request is a proxy.

How can I set this up from my unit test?


Solution

  • This has probably already been solved, but what worked for me was to use a mocking framework (like RhinoMocks):

    var contextMock = MockRepository.GenerateMock<HttpContextBase>();
    contextMock.Expect(x => x.Request).Return(new FakeHttpRequest("SOME_RELATIVE_URL", new Uri("http://to.somewhere.com"), new Uri("http://from.somewhere.com")));
    
    controller.ControllerContext.HttpContext = contextMock;