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

Testing a route attribute with an optional parameter #69

Open awj100 opened 8 years ago

awj100 commented 8 years ago

Thus far MvcRouteTester has worked well for verifying my WebAPI routes, but it appears to fall short in one situation.

I have the following method:

[Route("add-concept/{duplicateOverride:bool?}")]
[HttpPost]
public Concept AddConcept([FromBody] Concept concept, [FromUri] bool duplicateOverride = false)

As you can see, the duplicateOverride param is optional.

The following test passes:

[TestMethod]
public void Post__Create()
{
    const string route = "/dictionaries/add-concept/true";
    var verb = HttpMethod.Post;
    RouteAssert.HasApiRoute(Configuration, route, verb);
    Configuration.ShouldMap(route)
                 .To<DictionariesController>(verb, x => x.AddConcept(new Concept(), true));
}

But this test fails:

[TestMethod]
public void Post__Create_With_Default_Parameter()
{
    const string route = "/dictionaries/add-concept/";
    var verb = HttpMethod.Post;
    RouteAssert.HasApiRoute(Configuration, route, verb);
    Configuration.ShouldMap(route)
                 .To<DictionariesController>(verb, x => x.AddConcept(new Concept(), false));
}

The AssertionException says

Expected 'False', got no value for 'duplicateOverride' at url '/dictionaries/add-concept/'.

The only way I can think of testing the route without the duplicateOverride parameter is to refactor the method into two separate (chained) methods, with and without the parameter.

Or is there a better way?

AnthonySteele commented 8 years ago

For several reasons I have shelved this project: 1) There are lots of corner cases, like this, and lots of code to handle them, indicting that I was duplicating the routing engine (poorly) and a different approach would be better. 2) it used the internals of ASP MVC libraries, which broke on almost every minor release. keeping track of the route tester branches and packages was a pain. 3) All this is moot, everything has changed in ASP.NET MVC 5, I mean in ASP.Net Core 1.0 which will hopefully allow other approaches to this problem, such as this one