-
Notifications
You must be signed in to change notification settings - Fork 1
Test Helpers
The TestHelper library allows simpler unit testing on MVC.Net controllers than can be accomplished through standard mocking and subclassing. It is designed to reduce the amount of work required to test controllers, and therefore should be used by your test projects but not in your web server project. General Concepts Examples Testing Routes
##General Concepts##
One of the major advantages of the ASP.Net MVC framework is the ease with which it can be tested. While this is generally true, there are a number of areas where testing the framework becomes difficult to do. The TestHelper assists by providing a Controller Factory that creates controllers with internal data members correctly initialized. These include:
- HttpContext
- HttpRequest
- HttpResponse
- HttpSession
- Form
- TempData
- QueryString
- ApplicationPath
- PathInfo
It also provides a way to determine what parameters were passed to RenderView and RedirectToAction. Examples
More examples can be found in the Samples directory of the MVC Contrib project. In that directory is a project for MvcContrib.TestHelper.Sample which contains a very simple MVC.Net project and Test. There are a number of tests in the test project which detail how to use the Test Helper. A couple of them are included below. Testing data from RenderView method call
[Test]
public void ListControllerSelectsListView()
{
ActionResult result = controller.List();
result.AssertViewRendered().ForView("List");
}
Testing data from RedirectToAction method call
[Test]
public void AddFormStarShouldRedirectToList()
{
StarsController controller = new StarsController();
ActionResult result = controller.AddFormStar();
result.AssertActionRedirect().ToAction("List");
}
Testing data from Form and Session variables
[Test]
public void AddSessionStarShouldSaveFormToSession()
{
TestControllerBuilder builder = new TestControllerBuilder();
StarsController controller = new StarsController();
builder.InitializeController(controller);
//note that this is assigned before the controller action. This simulates the server filling out the form data from the request
builder.Form["NewStarName"] = "alpha c";
//this assumes that AddSessionStar takes the form data and adds it to the session
controller.AddSessionStar();
Assert.AreEqual("alpha c", controller.HttpContext.Session["NewStarName"]);
}
##Routes##
###Testing routes###
see MVCContrib.UnitTests\TestHelper\RoutesTest.cs
using System.Web.Mvc;
using System.Web.Routing;
using NUnit.Framework;
namespace MVCContrib.Application.UnitTests.TestHelper
{
/// <summary>
/// Summary description for UserRoutesTest
/// </summary>
[TestFixture]
public class UserRoutesTest
{
[TestFixtureSetUp]
public void Setup()
{
var routes = RouteTable.Routes;
routes.Clear();
routes.MapRoute(
"Default", // Route name
"{controller}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
[Test]
public void homeIndex()
{
"~/user"
.ShouldMapTo<HomeController>(action => action.Index());
}
[Test]
public void HomeShow()
{
"~/home"
.WithMethod(HttpVerbs.Put)
.ShouldMapTo<HomeController>(action => action.Index());
}
}
}