我遵循了Aaronaught和Scott H的指示,但它花了一些時間。我結束了這樣的事情。
public UrlHelper GetUrlHelper(
string fileName = "/",
string url="http://localhost",
string queryString="")
{
// Use routes from actual app
var routeCollection = new RouteCollection();
MvcApplication.RegisterRoutes(routeCollection);
//Make a request context
var request = new HttpRequest(fileName, url, queryString);
var response = new HttpResponse(new StringWriter());
var httpContext = new HttpContext(request, response);
var httpContextBase = new HttpContextWrapper(httpContext);
var requestContext = new RequestContext(httpContextBase, new RouteData());
// Make the UrlHelper with empty route data
return new UrlHelper(requestContext, routeCollection);
}
public void MoreFloorplans_ReturnsExpectedUrl()
{
var urlHelper = GetUrlHelper();
var actualResult = urlHelper.MoreFloorPlans(1,2);
Assert.AreEqual("/MoreFloorPlans/1/2", actualResult);
}
請注意,您應該測試您的擴展方法,而不是UrlHelper本身,因此,在設置中的RequestContext中的RouteData可能是超出了範圍。
謝謝我會試試看 – fregas 2010-04-19 21:32:56