Test Helper

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

One of the major advantages of the new MVC.Net 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 Form and Session variables

[Test]
publicvoid 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"]);
}

Comments

I was wondering if the TestHelper should set an HttpRequest, because I need to test an action that acts in different ways whether is a Post or Get http request. I solve it this way but i dont think this is the best one:

As somebody completely new to MVCContrib and eager to learn, the above examples seem rather obscure. "AddSessionStarShouldSaveFormToSession()" leaves me wondering what exactly we're trying to assert. I also find the language confusing; e.g.

"simulates the server filling out the form data from the request" - model binding?

"this assumes that AddSessionStar takes the form data and adds it to the session"

Who assumes [..]? Do you mean that the AddSessionStar method assumes that AddSessionStar takes the form data and adds it to the session? Do you mean, more simply, that "AddSessionStar adds form data to the session"? I find it confusing. I'll have a look at the samples project - maybe it's just me. :)

Is the TestControllerBuilder tied to RhinoMocks or can any other mock library be used? I'd like to use Moq (and do so to mock out my repository), but the TestControllerBuilder is throwing an error stating it can't find the Rhino Mocks assembly.