Using Asp.Net MVC routes from a non-web application (tests)

I am writing stack tests using Watin for an ASP.NET MVC application that uses T4MVC.

I find that I use "magic lines" in tests that I don't like.

[Given(@"I am on the sign up page")]
public void GivenIAmOnTheSignUpPage()
{
    string rootUrl = ConfigurationManager.AppSettings["RootUrl"];
    string fullUrl = string.Format("{0}/Authentication/Signup",rootUrl);
    WebBrowser.Current.GoTo(fullUrl);
}

I would rather use my T4MVC action results, as in an MVC application, something like this ...

[Given(@"I am on the sign up page")]
public void GivenIAmOnTheSignUpPage()
{
    WebBrowser.Current.GoTo(MVC.Authentication.SignUp().ToAbsoluteUrl());
 }

My ToAbsoluteUrlExtension Method

public static class RouteHelper
{
    private static UrlHelper _urlHelper;
    private static string _rootUrl;

    public static string ToAbsoluteUrl(this ActionResult result)
    {
        EnsureUrlHelperInitialized();

        var relativeUrl = _urlHelper.Action(result);
        return string.Format("{0}/{1}", _rootUrl, relativeUrl);
    }

    private static void EnsureUrlHelperInitialized()
    {
        if (_urlHelper==null)
        {
            _rootUrl = ConfigurationManager.AppSettings["RootUrl"];

            var request = new HttpRequest("/", _rootUrl, "");
            var response = new HttpResponse(new StringWriter());
            var context = new HttpContext(request,response);
            HttpContext.Current = context;
            var httpContextBase = new HttpContextWrapper(context);


            RouteTable.Routes.Clear();
            MvcApplication.RegisterRoutes(RouteTable.Routes);

            var requestContext = new RequestContext(httpContextBase, RouteTable.Routes.GetRouteData(httpContextBase));

            _urlHelper = new UrlHelper(requestContext, RouteTable.Routes);
        }
    }
}

What is the correct way to initialize RequestContext and RouteCollection so that I can generate test urls?

I am currently getting a NullReferenceException in a string var requestContext = new RequestContext(httpContextBase, RouteTable.Routes.GetRouteData(httpContextBase));. Is this the right way to a new queryContext query?

Or, if there is a better way to take an ActionResult (from T4MVC) and resolve it to an absolute URL, outside the web application, this is really what I am looking for.

+5
1
public static class RouteHelper
{
    private static UrlHelper _urlHelper;
    private static string _rootUrl;

    static RouteHelper()
    {
        var routes = new RouteCollection();
        MvcApplication.RegisterRoutes(routes);
        var req = new HttpRequest(string.Empty, "http://www.site.com", null);
        var res = new HttpResponse(null);
        var ctx = new HttpContext(req, res); // do not use HttpContext.Current
        var requestContext = new RequestContext(new HttpContextWrapper(ctx), 
            new RouteData());
        _urlHelper = new UrlHelper(requestContext, routes);
        _rootUrl = ConfigurationManager.AppSettings["RootUrl"];
    }

    public static string ToAbsoluteUrl(this ActionResult result)
    {
        return string.Format("{0}{1}", _rootUrl, _urlHelper.Action(result));
    }
}

. RouteCollection RouteTable.Routes, .

, HttpRequest HttpResponse. , . HttpContext ( HttpContext.Current xUnit). HttpContextWrapper, HttpContextBase.

RequestContext, RouteData. RouteCollection, UrlHelper. , Action , "/", RootUrl ( - value = "https://develop.site.com" ).

. , MVC. RegisterRoutes asax.

+5

All Articles