Asp.net mvc 无法在解决方案中完成。控制器上的属性才是真正需要测试的。这里没有显示。MapMvcAttributeRoutes是将这些属性转换为路由表数据的方式。假定它工作正常,因此不在测试中。我给出了一个简单的例子,它抓住了这个问题,并且基本上在一个web应用程序上进
Asp.net mvc 无法在解决方案中完成。控制器上的属性才是真正需要测试的。这里没有显示。MapMvcAttributeRoutes是将这些属性转换为路由表数据的方式。假定它工作正常,因此不在测试中。我给出了一个简单的例子,它抓住了这个问题,并且基本上在一个web应用程序上进,asp.net-mvc,unit-testing,asp.net-mvc-5,attributerouting,Asp.net Mvc,Unit Testing,Asp.net Mvc 5,Attributerouting,无法在解决方案中完成。控制器上的属性才是真正需要测试的。这里没有显示。MapMvcAttributeRoutes是将这些属性转换为路由表数据的方式。假定它工作正常,因此不在测试中。我给出了一个简单的例子,它抓住了这个问题,并且基本上在一个web应用程序上进行了测试。我认为Asp.Net框架算作前两方之一。谢谢,这至少是一个答案。将其扩展到一个完整的测试库(这就是我正在研究的)看起来复杂而复杂(而且脆弱)。希望有另一种方法,或者从可测试性的角度来看,这里的设计决策很糟糕。奇怪的是,只有网络路由受到
无法在解决方案中完成。控制器上的属性才是真正需要测试的。这里没有显示。MapMvcAttributeRoutes是将这些属性转换为路由表数据的方式。假定它工作正常,因此不在测试中。我给出了一个简单的例子,它抓住了这个问题,并且基本上在一个web应用程序上进行了测试。我认为Asp.Net框架算作前两方之一。谢谢,这至少是一个答案。将其扩展到一个完整的测试库(这就是我正在研究的)看起来复杂而复杂(而且脆弱)。希望有另一种方法,或者从可测试性的角度来看,这里的设计决策很糟糕。奇怪的是,只有网络路由受到影响。是的,不幸的是,我不知道这里有什么替代品。映射属性路由需要获取控制器列表(以便它可以找到它们的所有属性路由)。理想情况下,有一种方法可以替换提供控制器的服务,但在这个版本中我们没有这样做。我提交了一个bug来跟踪这个问题-谢谢你在这方面的帮助!很好的选择。不幸的是,为了使用独立的代理,我必须对许多测试进行硬编码,因为许多元素(例如,用于测试路由的RouteData)没有标记为可序列化的:(此代码已被改编并合并到MVCroudeTester中,在WebRouteTestApper类中:我注意到有关此解决方案脆弱的评论。尽管如此,这对我来说在MVC 5.1.2上不起作用。
MapMVCattributerRouteMethod
始终为空。手动检查程序集时我也找不到该方法。这是不是扩展方法应该存在于System.Web.Mvc
中的RouteCollectionAttributeRoutingExtensions
中?@MEMark:不幸的是,这只适用于Mvc 5.0;在5.1中被移到了另一个类中。因此可以找到一个关于5.1解决方案的问题。是的,它保证在下一个版本中很快会更改,就像routi一样ng在vNext中得到了受欢迎的重写。这就是为什么“在内部类中进行代码黑客攻击”远不如前门。不知道MS下次是否会明智地实际建造前门。他们直到现在还没有,将此作为唯一选项。缺点是我无法识别区域。因此,路径“/admin/service/accounts”其中admin是一个区域,映射到默认路由,因此:admin=controller,service=action
[Test]
public void HasRoutesInTable()
{
var routes = new RouteCollection();
routes.MapMvcAttributeRoutes();
Assert.That(routes.Count, Is.GreaterThan(0));
}
System.InvalidOperationException :
This method cannot be called during the applications pre-start initialization phase.
[TestClass]
public class MyTestClass
{
[TestMethod]
public void MyTestMethod()
{
// Move all files needed for this test into a subdirectory named bin.
Directory.CreateDirectory("bin");
foreach (var file in Directory.EnumerateFiles("."))
{
File.Copy(file, "bin\\" + file, overwrite: true);
}
// Create a new ASP.NET host for this directory (with all the binaries under the bin subdirectory); get a Remoting proxy to that app domain.
RouteProxy proxy = (RouteProxy)ApplicationHost.CreateApplicationHost(typeof(RouteProxy), "/", Environment.CurrentDirectory);
// Call into the other app domain to run route registration and get back the route count.
int count = proxy.RegisterRoutesAndGetCount();
Assert.IsTrue(count > 0);
}
private class RouteProxy : MarshalByRefObject
{
public int RegisterRoutesAndGetCount()
{
RouteCollection routes = new RouteCollection();
RouteConfig.RegisterRoutes(routes); // or just call routes.MapMvcAttributeRoutes() if that's what you want, though I'm not sure why you'd re-test the framework code.
return routes.Count;
}
}
}
public static class RouteCollectionExtensions
{
public static void MapMvcAttributeRoutesForTesting(this RouteCollection routes)
{
var controllers = (from t in typeof(HomeController).Assembly.GetExportedTypes()
where
t != null &&
t.IsPublic &&
t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase) &&
!t.IsAbstract &&
typeof(IController).IsAssignableFrom(t)
select t).ToList();
var mapMvcAttributeRoutesMethod = typeof(RouteCollectionAttributeRoutingExtensions)
.GetMethod(
"MapMvcAttributeRoutes",
BindingFlags.NonPublic | BindingFlags.Static,
null,
new Type[] { typeof(RouteCollection), typeof(IEnumerable<Type>) },
null);
mapMvcAttributeRoutesMethod.Invoke(null, new object[] { routes, controllers });
}
}
public class HomeControllerRouteTests
{
[Fact]
public void RequestTo_Root_ShouldMapTo_HomeIndex()
{
// Arrange
var routes = new RouteCollection();
// Act - registers traditional routes and the new attribute-defined routes
RouteConfig.RegisterRoutes(routes);
routes.MapMvcAttributeRoutesForTesting();
// Assert - uses MvcRouteTester to test specific routes
routes.ShouldMap("~/").To<HomeController>(x => x.Index());
}
}
public static void MapMvcAttributeRoutes(this RouteCollection routeCollection, Assembly controllerAssembly)
{
var controllerTypes = (from type in controllerAssembly.GetExportedTypes()
where
type != null && type.IsPublic
&& type.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase)
&& !type.IsAbstract && typeof(IController).IsAssignableFrom(type)
select type).ToList();
var attributeRoutingAssembly = typeof(RouteCollectionAttributeRoutingExtensions).Assembly;
var attributeRoutingMapperType =
attributeRoutingAssembly.GetType("System.Web.Mvc.Routing.AttributeRoutingMapper");
var mapAttributeRoutesMethod = attributeRoutingMapperType.GetMethod(
"MapAttributeRoutes",
BindingFlags.Public | BindingFlags.Static,
null,
new[] { typeof(RouteCollection), typeof(IEnumerable<Type>) },
null);
mapAttributeRoutesMethod.Invoke(null, new object[] { routeCollection, controllerTypes });
}