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());
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.
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.
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.
Alright... I did ping the ASP.NET team and asked them how they proceeded to do their tests.
You can't unit test without mocking the world. You have to do integration/functional tests.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With