MvcRouteTester icon indicating copy to clipboard operation
MvcRouteTester copied to clipboard

How do you test routes with constraints?

Open jkpindahbc opened this issue 12 years ago • 4 comments

There are some constraints on our routes that are causing the route tester to fail. Is there a known method for dealing with constraints?

jkpindahbc avatar Dec 31 '13 18:12 jkpindahbc

I haven't done a lot with constrains, so it's very possible that they don't work right. There are grey areas between matching the route but not being able to use it for the url, and not matching the route at all.

I tried a simple example and the test are passing: https://github.com/AnthonySteele/MvcRouteTester/blob/master/src/MvcRouteTester.Test/WebRoute/RouteConstraintTests.cs

What kind of constraint do you have and what result are you seeing?

AnthonySteele avatar Jan 03 '14 10:01 AnthonySteele

@jkpindahbc if you have chance, it would be helpful if you can fork the project, create a branch and a failing unit test to show the issue?

alexjamesbrown avatar Jan 03 '14 12:01 alexjamesbrown

What this came down to is the ability to override the custom constraints which use data from a database to validate url parameters. We are testing the routes, not the constraints, as such we developed a method to override the constraints (allowing the unit test code to override the match result). I will try to get the code on a branch this week. Thank you for the reply.

jkpindahbc avatar Jan 09 '14 22:01 jkpindahbc

This is how I'm testing at the moment but it would be nice for the framework to have some features to allow for this.

[Route("profile/{memberId:int:min(1)}")]
public ActionResult Index(int memberId) {....}

[Fact]
public void ProfileShouldNotMap()
{
    Action a = () => Routes
                .ShouldMap("/profile/0")
                .To<ProfileController>(x => x.Index(0));

    a.ShouldThrow<AssertionException>();
}

bapti avatar May 14 '14 10:05 bapti