AnthonySteele / MvcRouteTester

A library for unit testing ASP MVC route tables for both Web and API Routes
Apache License 2.0
105 stars 43 forks source link

"Global" controller routes not respected #56

Open justmara opened 9 years ago

justmara commented 9 years ago

Since MVC/WebAPI 5.2 there is an option to add single global route attribute to controller so all the actions in it will match:

    [RoutePrefix("files"), Route("{container}/{id}")]
    public class FilesController : ApiController
    {
        [HttpGet]
        public async Task<IHttpActionResult> GetDescription(string container, string id)
        {
            return Ok();
        }

        [HttpDelete]
        public async Task<IHttpActionResult> Delete(string container, string id)
        {
            return Ok();
        }
    }

This notation actually works flawlessly and all requests land as expected. But test fails:

    [TestMethod]
    public void GetDescription()
    {
        var expectations = new
        {
            controller = "Files",
            action = "GetDescription",
            container = "someContainer",
            id = "someFile"
        };
        RouteAssert.HasApiRoute(_host.Config, "/files/someContainer/someFile", HttpMethod.Get, expectations);
    }

with error: Expected 'someFile', got missing value for 'id' at url '/files/someContainer/someFile'. Simply copying the route attribute from controller to method declaration makes it pass:

    [HttpGet, Route("{container}/{id}")]
    public async Task<IHttpActionResult> GetDescription(string container, string id)