Comment tester les routes Asp.Net MVC

lors d’une phase de test, j’ai été amené a devoir tester un certain nombre de routes pour savoir quels Controllers et Actions etaient matchés, voici comment j’ai procédé.

Dans un premier temps il faut creer une classe HttpContext “bouchon” qui herite de HttpContextBase

public class MockHttpContext : HttpContextBase
{
private readonly string _relativeUrl;
private readonly Models.UserPrincipal _principal;
private readonly NameValueCollection _formParams;
private readonly NameValueCollection _queryStringParams;
private readonly HttpCookieCollection _cookies;
private readonly SessionStateItemCollection _sessionItems;


public MockHttpContext(string relativeUrl)
: this(relativeUrl, null, null, null, null, null)
{
}

public MockHttpContext(string relativeUrl, Models.UserPrincipal principal, NameValueCollection formParams, NameValueCollection queryStringParams, HttpCookieCollection cookies, SessionStateItemCollection sessionItems)
{
_relativeUrl = relativeUrl;
_principal = principal;
_formParams = formParams;
_queryStringParams = queryStringParams;
_cookies = cookies;
_sessionItems = sessionItems;
}

public override HttpRequestBase Request
{
get
{
return new MockHttpRequest(_relativeUrl, _formParams, _queryStringParams, _cookies);
}
}

public override IPrincipal User
{
get
{
return _principal;
}
set
{
throw new System.NotImplementedException();
}
}

public override HttpSessionStateBase Session
{
get
{
return null;
}
}

}
Et une classe HttpRequest “Bouchon”
	public class MockHttpRequest : HttpRequestBase
{
private readonly string _relativeUrl;
private readonly NameValueCollection _formParams;
private readonly NameValueCollection _queryStringParams;
private readonly HttpCookieCollection _cookies;

public MockHttpRequest(string relativeUrl, NameValueCollection formParams, NameValueCollection queryStringParams, HttpCookieCollection cookies)
{
_relativeUrl = relativeUrl;
_formParams = formParams;
_queryStringParams = queryStringParams;
_cookies = cookies;
}

public override NameValueCollection Form
{
get
{
return _formParams;
}
}

public override NameValueCollection QueryString
{
get
{
return _queryStringParams;
}
}

public override HttpCookieCollection Cookies
{
get
{
return _cookies;
}
}

public override string AppRelativeCurrentExecutionFilePath
{
get { return _relativeUrl; }
}

public override string PathInfo
{
get { return String.Empty; }
}


}
Et pour pouvoir tester n’importe quelle url, il faut commencer par la création du context :

var fakeContext = new Serialcoder.ERPStore.Web.MockHttpContext(“/test”);

puis passer le context au gestionnaire des routes  pour obtenir les valeurs sur la route determinée :

var currentRoute = RouteTable.Routes.GetRouteData(context);

on verifie le “controller” et l’”action” retournés :

var controller =(string) ((KeyValuePair<string, object>)currentRoute.Values["controller"]).Value;

var action = (string) ((KeyValuePair<string, object>)currentRoute.Values["action"]).Value;

il ne reste plus qu’a faire les comparaisons de test
 

3 commentaires:

Anonyme a dit…

Thanks for sharing this link, but unfortunately it seems to be down... Does anybody have a mirror or another source? Please reply to my post if you do!

I would appreciate if a staff member here at marc-chouteau.blogspot.com could post it.

Thanks,
Peter

Marc Chouteau a dit…

What link ?

Anonyme a dit…

Brilliant site, I had not noticed marc-chouteau.blogspot.com previously during my searches!
Continue the good work!