Skip to content

Instantly share code, notes, and snippets.

@benfoster
Created January 7, 2013 11:08
Show Gist options
  • Save benfoster/4474276 to your computer and use it in GitHub Desktop.
Save benfoster/4474276 to your computer and use it in GitHub Desktop.
Inbound route testing with ASP.NET MVC
using NSubstitute;
using NUnit.Framework;
using System.Linq;
using System.Web;
using System.Web.Routing;
namespace Fabrik.Web.Hosted.Specs
{
[TestFixture]
public class InboundRouteTests
{
RouteCollection routes;
[TestFixtureSetUp]
public void FixtureSetUp()
{
routes = new RouteCollection();
RouteConfig.RegisterRoutes(routes);
}
// Pages
[Test]
public void Home()
{
Request("~/").ShouldMapTo(new { controller = "pages", action = "home" });
}
[Test]
public void PageDetails()
{
Request("~/pages/contact").ShouldMapTo(new { controller = "pages", action = "details", slug = "contact" });
}
[Test]
public void PageDetailsSegments()
{
Request("~/pages/about/me").ShouldMapTo(new { controller = "pages", action = "details", slug = "about/me" });
}
// Portfolio
[Test]
public void Portfolio()
{
Request("~/portfolio").ShouldMapTo(new { controller = "portfolio", action = "list" });
}
[Test]
public void PortfolioProjectsPaged()
{
Request("~/portfolio/page/2").ShouldMapTo(new { controller = "portfolio", action = "list", page = 2 });
}
[Test]
public void PortfolioTaggedProjects()
{
Request("~/portfolio/tagged/movies").ShouldMapTo(new { controller = "portfolio", action = "tagged", tag = "movies" });
}
[Test]
public void PortfolioTaggedProjectsPaged()
{
Request("~/portfolio/tagged/movies/page/2").ShouldMapTo(new { controller = "portfolio", action = "tagged", tag = "movies", page = 2 });
}
[Test]
public void PortfolioTags()
{
Request("~/portfolio/tags").ShouldMapTo(new { controller = "portfolio", action = "tags" });
}
[Test]
public void PortfolioProjectDetails()
{
Request("~/portfolio/a-test-project").ShouldMapTo(new { controller = "portfolio", action = "details", slug = "a-test-project" });
}
[Test]
public void PortfolioProjectDetailsSegements()
{
Request("~/portfolio/archive/a-test-project").ShouldMapTo(new { controller = "portfolio", action = "details", slug = "archive/a-test-project" });
}
// Blog
[Test]
public void Blog()
{
Request("~/blog").ShouldMapTo(new { controller = "blog", action = "list" });
}
[Test]
public void BlogPostsPaged()
{
Request("~/blog/page/2").ShouldMapTo(new { controller = "blog", action = "list", page = 2 });
}
[Test]
public void BlogTaggedPosts()
{
Request("~/blog/tagged/updates").ShouldMapTo(new { controller = "blog", action = "tagged", tag = "updates" });
}
[Test]
public void BlogTaggedPostsPaged()
{
Request("~/blog/tagged/updates/page/2").ShouldMapTo(new { controller = "blog", action = "tagged", tag = "updates", page = 2 });
}
[Test]
public void BlogPostTags()
{
Request("~/blog/tags").ShouldMapTo(new { controller = "blog", action = "tags" });
}
[Test]
public void BlogPostDetails()
{
Request("~/blog/a-test-post").ShouldMapTo(new { controller = "blog", action = "details", slug = "a-test-post" });
}
[Test]
public void BlogPostDetailsSegements()
{
Request("~/blog/archive/a-test-post").ShouldMapTo(new { controller = "blog", action = "details", slug = "archive/a-test-post" });
}
// Categories
[Test]
public void Categories()
{
Request("~/categories").ShouldMapTo(new { controller = "categories", action = "list" });
}
[Test]
public void CategoriesPaged()
{
Request("~/categories/page/2").ShouldMapTo(new { controller = "categories", action = "list", page = 2 });
}
[Test]
public void CategoryDetails()
{
Request("~/commercials").ShouldMapTo(new { controller = "categories", action = "details", slug = "commercials" });
}
[Test]
public void CategoryDetailsPaged()
{
Request("~/commercials/page/2").ShouldMapTo(new { controller = "categories", action = "details", slug = "commercials", page = 2 });
}
// Errors
[Test]
public void Error()
{
Request("~/error").ShouldMapTo(new { controller = "error", action = "index" });
}
[Test]
public void ErrorNotFound()
{
Request("~/error/notfound").ShouldMapTo(new { controller = "error", action = "notfound" });
}
[Test]
public void CatchAll()
{
Request("~/foo/bar").ShouldMapTo(new { controller = "error", action = "notfound" });
}
private RouteValueDictionary Request(string virtualPath)
{
var httpContext = Substitute.For<HttpContextBase>();
httpContext.Request.ApplicationPath.ReturnsForAnyArgs("/");
httpContext.Request.AppRelativeCurrentExecutionFilePath.Returns(virtualPath);
var routeData = routes.GetRouteData(httpContext);
return routeData.Values;
}
}
public static class RouteShouldExtensions
{
public static void ShouldMapTo(this RouteValueDictionary routeValues, object expectedRouteValues)
{
var expectedDictionary = new RouteValueDictionary(expectedRouteValues);
bool isEqual = expectedDictionary.All(x => x.Value.ToString() == (routeValues[x.Key].ToString()));
isEqual.ShouldBeTrue();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment