Welcome Guest, you are in: Login

Castle Project


Navigation (MonoRail)

Search the wiki


Unit Testing

Modified on 2010/09/24 17:05 by Symon Rottem Categorized as Uncategorized


Previously, MonoRail used the ASP.NET infrastructure to run a web site and run tests against it. This worked for simple scenarios but didn't scale well or allow access to all aspects of the RailsEngineContext.

During RC2, BaseControllerTest was added to the trunk. BaseControllerTest exposed all of the properties of the MonoRail pipeline so that you can now inspect values as if running in a web context. This allowed us to mock the RailsEngineContext and inject mocks into the pipeline.

The TestSupport Assembly

Castle.MonoRail.TestSupport was created to enable easy testing of MonoRail projects. Classes in this namespace are for performing tests on MonoRail Controllers. It exposes the PropertyBag, Flash and Session dictionaries so you can write assertions for their contents. You also have access to MockRailsEngineContext via the Context property to insert values to be used during the Controller execution.

In order to use the ASP.Net runtime for deprecated AbstractMRTestCase, the assembly Castle.MonoRail.TestSupport.dll must be registered in the GAC. If you have installed Castle using the MSI distribution this was already done for you. Otherwise, execute:

> gacutil /i Castle.MonoRail.TestSupport

Setting Up a Test Project

To set up a test project perform the following steps:

  • Create a Class Library project (usually you are going to use the same solution of the web project)

  • Add references to:




  • Create test case classes extending BaseControllerTest

  • Call the PrepareController method passing Controller Instance, Area, Controller Name and Action Name.

  • Make Assertion calls on PropertyBag or other parts of the Controller that is under test. You can access the RailsEngineContext from BaseControllerTest.Context

A simple example from MonoRail test case

The following class is a snippet of one of the MonoRail test cases:

public class BasicFunctionalityTestCase : BaseControllerTest
	public void SimpleControllerAction()
		SimpleController simpleController = new SimpleController();
		PrepareController(simpleController, "areaName", "simplecontroller", "index");


		// Some Assertions here

	public void Flash()
		SimpleController simpleController = new SimpleController();
		PrepareController(simpleController, "areaName", "simplecontroller", "someotheraction");



	public void Redirect()
		SimpleController simpleController = new SimpleController();
		PrepareController(simpleController, "areaName", "simplecontroller", "redirectaction");


		Assert.IsNotNull("controller/action.rails", Response.RedirectedTo);

	public void PropertyBag()
		SimpleController simpleController = new SimpleController();
		PrepareController(simpleController, "areaName", "simpleController", "propertybagaction");



	public void CurrentUser()
		SimpleController simpleController = new SimpleController();
		PrepareController(simpleController, "areaName", "simpleController", "currentuseraction");



For a more detailed example see TDDingControllers on using.castleproject.org

For more documentation, please see the Castle.MonoRail.TestSupport namespace classes .

ScrewTurn Wiki version Some of the icons created by FamFamFam.