I'm writing specflow tests using Watin, for an Asp.Net MVC application which uses T4MVC.
I find myself using "magic string" urls in the tests, which 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 much rather use my T4MVC Action Results like I do in the MVC App, something like this...
[Given(@"I am on the sign up page")]
public void GivenIAmOnTheSignUpPage()
{
WebBrowser.Current.GoTo(MVC.Authentication.SignUp().ToAbsoluteUrl());
}
My ToAbsoluteUrl
Extension 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 the RequestContext and RouteCollection so that I can generate my test URLs?
Currently I receive a NullReferenceException on the line var requestContext = new RequestContext(httpContextBase, RouteTable.Routes.GetRouteData(httpContextBase));
. Is that the right way to new up a requestContext?
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.
Configure a Route Every MVC application must configure (register) at least one route configured by the MVC framework by default. You can register a route in RouteConfig class, which is in RouteConfig. cs under App_Start folder. The following figure illustrates how to configure a route in the RouteConfig class .
Routing in ASP.NET MVC cs file in App_Start Folder, You can define Routes in that file, By default route is: Home controller - Index Method. routes. MapRoute has attributes like name, url and defaults like controller name, action and id (optional).
In MVC, routing is a process of mapping the browser request to the controller action and return response back. Each MVC application has default routing for the default HomeController. We can set custom routing for newly created controller.
The Default route maps the first segment of a URL to a controller name, the second segment of a URL to a controller action, and the third segment to a parameter named id. The Default route maps this URL to the following parameters: controller = Home. action = Index.
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));
}
}
The static constructor sets up your private fields. I chose to use a new RouteCollection, instead of using the static RouteTable.Routes property, but you might be able to.
I don't think the constructors for the HttpRequest and HttpResponse matter. I just passed in some strings to get them to construct without throwing an exception. Use those to construct a brand new HttpContext (don't use HttpContext.Current when running from xUnit). You can then put it into an HttpContextWrapper to get your HttpContextBase reference.
Construct a new RequestContext, passing in your base wrapper and a new RouteData instance. Use that, along with your previous RouteCollection to construct the UrlHelper. Note that its Action method will return strings prepended with "/", so you should leave that out of our RootUrl appSetting (so use something like value="https://develop.site.com" without the trailing slash).
Note this will not work for routes defined in MVC areas. For that, you need to register the areas in addition to calling RegisterRoutes in global asax.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With