Skip to content

Instantly share code, notes, and snippets.

@BryanWilhite
Created June 22, 2015 17:07
Show Gist options
  • Select an option

  • Save BryanWilhite/d72a8cf8cfd07c0e3c91 to your computer and use it in GitHub Desktop.

Select an option

Save BryanWilhite/d72a8cf8cfd07c0e3c91 to your computer and use it in GitHub Desktop.
ShouldTestRoutes() uses MvcRouteTester.Mvc5.2
/// <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