【问题标题】:Unit Testing MVC 4 routes using AttributeRouting and MvcRouteTester使用 AttributeRouting 和 MvcRouteTester 对 MVC 4 路由进行单元测试
【发布时间】:2013-10-18 18:56:31
【问题描述】:

所以我一直在研究这个问题,但我找不到解决我的问题的方法

我有一个 MVC4/Web API 应用程序,我正在使用 AttributeRouting 3.5.6

我的应用运行良好。

我能够使用以下响应对 Web API 路由进行单元测试

AttributeRouting not working with HttpConfiguration object for writing Integration tests

我现在想做的是对我的 MVC4 路由进行单元测试,但我还没有找到方法

我发现了以下问题,我想知道是否有解决方法

https://github.com/mccalltd/AttributeRouting/issues/64

基本上,问题似乎是为我的单元测试从内存中的属性加载路由

这是我迄今为止尝试过的:

routes.MapAttributeRoutes(x =>
{
    x.AddRoutesFromAssembly(typeof(HomeController).Assembly);
    x.AddRoutesFromAssemblyOf<HomeController>();

    x.AddRoutesFromController(typeof(HomeController));
    x.AddRoutesFromController<HomeController>();
    x.AddRoutesFromControllersOfType(typeof(Controller));
    x.AddRoutesFromControllersOfType<Controller>();
});
  • 当我使用任何 Assembly 方法时,路由集合为空

  • 当我使用任何 Controller 方法时,我会收到以下异常:

    System.Security.VerificationException:方法 AttributeRouting.Web.Mvc.Configuration.AddRoutesFromControllersOfType:类型参数“MyNamespace.Controllers.HomeController”违反了类型参数“T”的约束。

它必须是一种方式,因为当我运行应用程序时它工作得很好,我所有的路由都注册在RoutesCollection

【问题讨论】:

    标签: c# asp.net-mvc-4 attributerouting


    【解决方案1】:

    将以下内容添加到测试项目的 app.config 中

    <configuration>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="4.0.0.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    </configuration>
    

    【讨论】:

    • 看起来 MvcRouteTester 包现在默认添加了这个。
    猜你喜欢
    • 2020-08-13
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多