Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing routing in ASP.NET Core 1.0 (ex MVC 6)

As the architecture of ASP.NET Core 1.0 (ex MVC 6 / ASP.NET 5.0) changed significantly, how would one go about unit testing the routing?

As an example, I like the libraries such as this one (as for <= MVC 5): https://github.com/AnthonySteele/MvcRouteTester

Something down the lines of fluent extension methods:

routes.ShouldMap("/").To<HomeController>(x => x.Index()); 
like image 451
Denis Biondic Avatar asked Aug 20 '15 09:08

Denis Biondic


People also ask

What is routing in ASP.NET Core MVC?

Routing is responsible for matching incoming HTTP requests and dispatching those requests to the app's executable endpoints. Endpoints are the app's units of executable request-handling code. Endpoints are defined in the app and configured when the app starts.

What is routing and types of routing in MVC?

Routing is how ASP.NET MVC matches a URI to an action. MVC 5 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web application.

How routing is done in the MVC pattern?

In MVC, routing is a process of mapping the browser request to the controller action and return response back. Each MVC application has default routing for the default HomeController. We can set custom routing for newly created controller. The RouteConfig.


1 Answers

Alright... I did ping the ASP.NET team and asked them how they proceeded to do their tests.

Short answer

You can't unit test without mocking the world. You have to do integration/functional tests.

Slightly longer answer

Routing can come from many places (attributes, pre-defined routes, areas, etc) and can even be altered by middleware. Including all those scenarios in a test would need to setup some massive dependency tree while there is an easier way to test it.

How they do it.

  • Startup.cs
  • RoutingTests.cs
  • TestResponseGenerator.cs

Basically, they are creating a TestStartup.cs, self-hosting the app in the test process and hitting itself to see how it behaves. They override the results and... that's pretty much it.

I think I've given you all the possible tools here to actually bootstrap something for yourself.

like image 61
Maxime Rouiller Avatar answered Oct 23 '22 18:10

Maxime Rouiller