Let's say we have registered routes in our ASP.NET MVC application:
How can we test this routes using NUnit testing framework and FakeItEasy mocking framework?
Here's an example of how this can be done:
routes.MapTrailingSlashRoute(
"Topic", // Route name
"news/{url}", // URL with parameters
new { controller = "Topic", action = "Index" } // Parameter defaults
);
routes.MapTrailingSlashRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index" , id = "" } // Parameter defaults
);
How can we test this routes using NUnit testing framework and FakeItEasy mocking framework?
Here's an example of how this can be done:
[TestFixture]
[Category("Unit")]
public class when_using_registered_routes
{
[TestFixtureSetUp]
public void Prepare()
{
MvcApplication.RegisterRoutes(RouteTable .Routes);
}
[Test]
[TestCase("~/", "Home", "Index" )]
[TestCase( "~/news/testing-routes-in-mvc/", "Topic" , "Index" )]
[TestCase("~/discussion/all/", "Discussion", "All")]
[TestCase( "~/discussion/widget/", "Discussion" , "Widget" )]
public void should_return_expected_controller_and_action(string path, string expectedController, string expectedAction)
{
var httpContext = A.Fake<HttpContextBase>();
A.CallTo(() => httpContext.Request.AppRelativeCurrentExecutionFilePath).Returns(path);
var routeData = RouteTable.Routes.GetRouteData(httpContext);
Assert.That(routeData.Values["controller"].ToString().ToLower(), Is.EqualTo(expectedController.ToLower()));
Assert.That(routeData.Values["action"].ToString().ToLower(), Is.EqualTo(expectedAction.ToLower()));
}
}
No comments:
Post a Comment