在我的 ASP.NET MVC 3 应用程序上,我定义了一个路由约束,如下所示:
public class CountryRouteConstraint : IRouteConstraint {
private readonly ICountryRepository<Country> _countryRepo;
public CountryRouteConstraint(ICountryRepository<Country> countryRepo) {
_countryRepo = countryRepo;
}
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) {
//do the database look-up here
//return the result according the value you got from DB
return true;
}
}
我在我的应用程序上使用 Ninject 作为 IoC 容器,它实现IDependencyResolver
并注册了我的依赖项:
private static void RegisterServices(IKernel kernel) {
kernel.Bind<ICountryRepository<Country>>().
To<CountryRepository>();
}
如何以依赖注入友好的方式使用此路由约束?
编辑
我找不到通过这种对单元测试的依赖的方法:
[Fact]
public void country_route_should_pass() {
var mockContext = new Mock<HttpContextBase>();
mockContext.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/countries/italy");
var routes = new RouteCollection();
TugberkUgurlu.ReservationHub.Web.Routes.RegisterRoutes(routes);
RouteData routeData = routes.GetRouteData(mockContext.Object);
Assert.NotNull(routeData);
Assert.Equal("Countries", routeData.Values["controller"]);
Assert.Equal("Index", routeData.Values["action"]);
Assert.Equal("italy", routeData.Values["country"]);
}