Friday, August 5, 2016

Unit Testing ASP.NET Web API using Attribute-based Routing

If you review any Unit Test code samples for ASP.NET Web API, you will find examples on how to Unit Tests when using CreatedAtRoute but they only refer to using the Default Routing rules available in WebApiConfig.cs.

So what do you do if you are using Attribute-based routing?

Well, while this scenario is poorly documented, there is a solution for this as well!

You start by using the Route Name attribute on  your Post method as follows:


Now, if you want to Unit Test that code, you can do something like the following:

That is all that is needed in order to support Unit Testing with Attribute-based routing!

No comments:

Post a Comment