Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Asp.Net MVC Routes from a non-web application (tests)
    primarykey
    data
    text
    <p>I'm writing specflow tests using Watin, for an Asp.Net MVC application which uses T4MVC.</p> <p>I find myself using "magic string" urls in the tests, which I don't like.</p> <pre><code>[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); } </code></pre> <p>I would much rather use my T4MVC Action Results like I do in the MVC App, something like this...</p> <pre><code>[Given(@"I am on the sign up page")] public void GivenIAmOnTheSignUpPage() { WebBrowser.Current.GoTo(MVC.Authentication.SignUp().ToAbsoluteUrl()); } </code></pre> <p>My <code>ToAbsoluteUrl</code> Extension Method</p> <pre><code>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); } } } </code></pre> <p>What is the correct way to initialize the RequestContext and RouteCollection so that I can generate my test URLs?</p> <p>Currently I receive a NullReferenceException on the line <code>var requestContext = new RequestContext(httpContextBase, RouteTable.Routes.GetRouteData(httpContextBase));</code>. Is that the right way to new up a requestContext?</p> <p>Or if there is a better way to take an ActionResult (from T4MVC) and resolve it to an absolute url, outside of a web app, that's really what I'm looking for.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload