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

How do you test routes with constraints? #32

Open jkpindahbc opened 10 years ago

jkpindahbc commented 10 years ago

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

AnthonySteele commented 10 years ago

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?

alexjamesbrown commented 10 years ago

@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?

jkpindahbc commented 10 years ago

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.

bapti commented 10 years ago

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>();
}