Frage

So I have been researching a lot about this but I cannot find a workaround for my problem

I have a MVC4/Web API application and I'm using AttributeRouting 3.5.6

My app works fine.

I was able to unit test the Web API routes using the following response

AttributeRouting not working with HttpConfiguration object for writing Integration tests

What I want to do now is unit test my MVC4 routes but I have not found a way to do it

I found the following issue and I'm wondering if there's a workaround out there

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

Basically the problem seems to be loading the routes from the attributes in memory for my unit tests

This is what I have tried so far:

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>();
});
  • When I useany of the Assembly methods the routes collection is empty

  • When I use any of the Controller methods I receive the following exception:

    System.Security.VerificationException: Method AttributeRouting.Web.Mvc.Configuration.AddRoutesFromControllersOfType: type argument 'MyNamespace.Controllers.HomeController' violates the constraint of type parameter 'T'.

It has to be a way because when I run the application it works just fine, all my routes are registered in the RoutesCollection

War es hilfreich?

Lösung

Add the following to the app.config of your test project

<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>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top