After the past 2 1/2 posts I’ve migrated my unit tests to use the BaseControllerTest abstract class that originated from the Eleutian guys, but is now a part of the Castle MonoRail trunk download.
Here is my ExampleControllerTests class:
1: using Castle.MonoRail.TestSupport;
2: using JasonMeridth.Controllers;
3: using NUnit.Framework;
5: namespace JasonMeridth.Tests.Controllers
8: public class ExampleControllerTests : BaseControllerTest
10: private ExampleController exampleController;
13: public void Setup_context()
15: exampleController = new ExampleController();
16: PrepareController(exampleController, "", "Example", "helloworld");
20: public void Controller_should_not_be_null()
23: Assert.AreEqual(@"Examplehelloworld", exampleController.SelectedViewName);
27: public void Should_see_hello_world_message()
33: string message = exampleController.PropertyBag["message"].ToString();
35: Assert.AreEqual("Hello World", message);
An excellent post by Sean Chambers
shows how to test controllers also. As Sean states the PrepareController
method is the core method we need for our test wire-up. You pass this method the controller, the area, and the action and it sets up the IRailsEngineContext by creating instances of mock classes that will respond to your test. Looking at the trunk source code is always beneficial, so I highly suggest it.
As many of the developers who use MonoRail regularly have stated in many blogs, ViewComponents are like re-usable controls. I had a situation where I wanted today’s date to show up on my menu on every page. A ViewComponent came to mind.
To create a view component you have to add the following folders to your project:
——–Component.cs (in my case, MenuComponent.cs)
————-<component name> folder (in my case, menucomponent)
The default.vm is the expected output of your view component. Just like the action views, this is what will be displayed when your component is used.
The code for the MenuComponent.cs is: (VERY simple)
1: using System;
2: using Castle.MonoRail.Framework;
4: namespace JasonMeridth.ViewComponents
6: public class MenuComponent : ViewComponent
8: public override void Render()
10: PropertyBag["today"] = DateTime.Now;
All I want is today’s date. The rest is in the NVelocity file:
1: <div id="menu">
3: <li class="first"><a href="../../home/index.rails">Home</a></li>
4: <li><a href="http://www.lostechies.com/blogs/jason_meridth" target="_blank">My Blog</a></li>
5: <li><a href="../../contact/contactform.rails">Contact</a></li>
8: <div id="date">$today.ToLongDateString()</div>
Notice the $today.ToLongDateString() section. $today is coming from Today’s date which was placed in the PropertyBag by the controller.
Easy. And now to inject the view component into my default.vm for layouts:
1: <div id="header">
2: <img src="../Content/images/lucas_header.JPG" />
8: <div id="primarycontent">
11: <!-- primary content start -->
13: <!-- primary content end -->
Please refer back to my first post for the whole file and you’ll notice the code that was removed and placed into the view component’s default.vm and the new line of code, #component(MenuComponent) in the default layout default.vm. It’s that simple.
I hope this helps. This was ViewComponents 101. I will delve deeper into some tips and tricks with these later in these posts.
***Joey does have a post about separating the ViewComponents into a new assembly, therefore allowing you distribute your components and make them re-usable.
Some excellent helper posts from other developers:
1. JoeyDotNet: Post 1, Post 2, Post 3 (I swear this isn’t programmer worship, he’s just my best resource right now, plus he’s put out 2 production MonoRail sites – a good resource)
2. Sean Chambers: Post 1
NEXT POST: Validation
Monorail #1:Reasons, Setup, and First Output
Monorail #2:Layouts and Rescues
Post Footer automatically generated by Add Post Footer Plugin for wordpress.
This entry was posted in castle
. Bookmark the permalink
. Follow any comments here with the RSS feed for this post