Ayende @ Rahien

It's a girl

Testing Presentation Logic

I wanted to comment to this post from Scott McMaster, where he responds to my SoC post. What caught my eye was this:

Below the surface, a lot of the linked-in discussion seems to hinge on whether the banding logic qualifies as "business logic" or "presentation logic".  For the purpose here today, I don't much care what kind of "logic" it is, but it IS sufficiently non-trivial to require unit testing.  And if you bury it inside the page markup, you will have an extremely difficult time doing that.

I don't agree, it is extremely easy to test a view in MonoRail. In this case, I would do it with something like this:

[Test]
public void ShowOrdersView_WithMoreThanTenRows_WillShowRunningTotal()
{
	List<Order> orders = new List<Order>();
	for(int i=0;i<15;i++)
	{
		orders.Add( TestGenerator.CreateOrderWithCost(500) );
	}
	XmlDocument viewDOM = EvaluateViewAndReturnDOM( "ShowOrdersView", new Parameters("orders", orders));
	int index = 1;
	int totalSoFar = 0;
	foreach(XmlNode tr in viewDOM.SelectSingleNode("//table[@id='orderSummary']/tr"))
	{
		if(index%10 != 0)
		{
			Assert.IsNotNull(tr.SelectSingleNode("td/value()=='500'"));
			totalSoFar += 500;
		}
		else
		{
			Assert.Contains(td.Children[0].InnerText, "Running Total");
			Assert.Contains(td.Children[1].InnerText, totalSoFar.ToString());
		}
		index+=1;
	}
	Assert.AreEqual(15, index, "Not enough rows were found");
}

As you probably have figured out, this is an semi-integration test, and it tests the output of the view without involving anything else. The EvaluateViewAndReturnDOM will evaluate the view and will use SgmlReader to return an XmlDocument that can be easily checked.

 

Comments

Jose
07/27/2007 05:46 PM by
Jose

I agree with you. The method EvaluateViewAndReturnDOM looks interesting, is it part of MonoRail or is you idea?

Ayende Rahien
07/27/2007 07:38 PM by
Ayende Rahien

It is my own idea, it is very simple, actually, it just use the JustView way and pipe it to an SgmlReader:

https://svn.castleproject.org/svn/castle/trunk/MonoRail/Castle.MonoRail.Views.Brail.Tests/JustViewFixture.cs

Vladimir
08/07/2007 11:01 AM by
Vladimir

Ayende, code in JustViewFixture.cs won't work in when brail view contains ViewComponent. I've just added a new issue in Castle JIRA

Comments have been closed on this topic.