Created
June 22, 2015 17:07
-
-
Save BryanWilhite/d72a8cf8cfd07c0e3c91 to your computer and use it in GitHub Desktop.
ShouldTestRoutes() uses MvcRouteTester.Mvc5.2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// <summary> | |
| /// Test context extensions: should test routes. | |
| /// </summary> | |
| /// <param name="context">The context.</param> | |
| /// <param name="pathToJson">The path to json.</param> | |
| public static void ShouldTestRoutes(this TestContext context, Type controllerType, string pathToJson) | |
| { | |
| var routes = new RouteCollection(); | |
| routes.MapAttributeRoutesInAssembly(controllerType); | |
| Assert.IsTrue(routes.Any(), "The expected attribute routes are not here."); | |
| context.ShouldFindFile(pathToJson); | |
| var json = File.ReadAllText(pathToJson); | |
| var definition = new[] | |
| { | |
| new | |
| { | |
| route = string.Empty, | |
| controller = string.Empty, | |
| action = string.Empty, | |
| isNonRoute = false | |
| } | |
| }; | |
| var data = JsonConvert.DeserializeAnonymousType(json, definition); | |
| Assert.IsTrue(data.Any(), "The expected array of JSON data is not here."); | |
| data.Where(i => !i.isNonRoute).ForEachInEnumerable(i => | |
| { | |
| var expected = new | |
| { | |
| controller = (string.IsNullOrEmpty(i.controller))? | |
| controllerType.Name.Replace("Controller", string.Empty) | |
| : | |
| i.controller, | |
| action = i.action | |
| }; | |
| context.WriteLine("Testing route {0} to {1}.{2}...", i.route, expected.controller, i.action); | |
| RouteAssert.HasRoute(routes, i.route, expected); | |
| }); | |
| data.Where(i => i.isNonRoute).ForEachInEnumerable(i => | |
| { | |
| context.WriteLine("Testing non-route {0}...", i.route); | |
| RouteAssert.NoRoute(routes, i.route); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment