Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 带约束的单元测试ASP MVC路由_Asp.net Mvc 3_Unit Testing_Routing_Mvccontrib - Fatal编程技术网

Asp.net mvc 3 带约束的单元测试ASP MVC路由

Asp.net mvc 3 带约束的单元测试ASP MVC路由,asp.net-mvc-3,unit-testing,routing,mvccontrib,Asp.net Mvc 3,Unit Testing,Routing,Mvccontrib,我的路线定义如下: routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional, area = "" }, // Parameter defaults

我的路线定义如下:

routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional, area = "" }, // Parameter defaults
            new { home = new HomePageConstraint() }
        );

public class HomePageConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        return !httpContext.Request.RawUrl.StartsWith("/home", StringComparison.InvariantCultureIgnoreCase);
    }
}
[Test]
public void Home_Load_Homepage()
{
    "~/".ShouldMapTo<HomeController>(x => x.Index());
}
我试着这样测试它:

routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional, area = "" }, // Parameter defaults
            new { home = new HomePageConstraint() }
        );

public class HomePageConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        return !httpContext.Request.RawUrl.StartsWith("/home", StringComparison.InvariantCultureIgnoreCase);
    }
}
[Test]
public void Home_Load_Homepage()
{
    "~/".ShouldMapTo<HomeController>(x => x.Index());
}
[测试]
公共无效主页加载主页()
{
“~/”.ShouldMapTo(x=>x.Index());
}
我遇到的问题是
httpContext
为空,因此测试失败。如何将http上下文注入到约束中?

最后我做了以下操作:

var context = new FakeHttpContext("~/");
var fakeRequest = new FakeRequest("~/", new Uri("http://localhost/"), new Uri("http://localhost/"));
context.SetRequest(fakeRequest);

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

route.ShouldMapTo<HomeController>(x => x.Index());
var context=new FakeHttpContext(“~/”);
var fakeRequest=new fakeRequest(“~/”,新Uri(“http://localhost/“”,新Uri(“”)http://localhost/"));
SetRequest(fakeRequest);
var route=RouteTable.Routes.GetRouteData(上下文);
route.ShouldMapTo(x=>x.Index());
最后我做到了:

var context = new FakeHttpContext("~/");
var fakeRequest = new FakeRequest("~/", new Uri("http://localhost/"), new Uri("http://localhost/"));
context.SetRequest(fakeRequest);

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

route.ShouldMapTo<HomeController>(x => x.Index());
var context=new FakeHttpContext(“~/”);
var fakeRequest=new fakeRequest(“~/”,新Uri(“http://localhost/“”,新Uri(“”)http://localhost/"));
SetRequest(fakeRequest);
var route=RouteTable.Routes.GetRouteData(上下文);
route.ShouldMapTo(x=>x.Index());