Search code examples
asp.net-mvcmockingrhino-mocks

Testing outbound url generation in ASP.NET MVC with Rhino Mocks


I'm struggling to find a complete example of how to create the the request context necessary to test outbound urls with Rhino Mocks.

I've got an example of using Moq that I have attempted to convert to Rhino Mocks but I'm not having much luck.

    private static HttpContextBase MakeMockHttpContext(string url)
    {
        var mocks = new MockRepository();
        var mockHttpContext = mocks.StrictMock<HttpContextBase>();
        // Mock the request
        var mockRequest = mocks.StrictMock<HttpRequestBase>();
        mockHttpContext.Expect(x => x.Request).Return(mockRequest);
        mockRequest.Expect(x => x.AppRelativeCurrentExecutionFilePath).Return(url);
        // Mock the response
        var mockResponse = mocks.StrictMock<HttpResponseBase>();
        mockHttpContext.Expect(x => x.Response).Return(mockResponse);
        mockResponse.Stub(r => r.ApplyAppPathModifier(Arg<string>.Is.Anything))
            .Do(new Func<string, string>(s => s));

        return mockHttpContext;
    }

    private string GenerateUrl(object values)
    {
        RouteCollection routeConfig = new RouteCollection();
        Global.RegisterRoutes(routeConfig);
        var mockContext = MakeMockHttpContext(null);
        var requestContext = new RequestContext(mockContext, new RouteData());

        return UrlHelper.GenerateUrl(null, null, null, 
            new RouteValueDictionary(values), routeConfig, requestContext, true);
    }

    [Test]
    public void Catalog_index_returns_root_url()
    {
        GenerateUrl(new { controller = "Catalog", action = "Index" }).ShouldEqual("/");
    }

Any help would be appreciated.


Solution

  • This should get you started... Testing URL Generation on Routes in MVC Framework