Using mbUnit’s RowTest Attribute in a TDD Scenario

Wednesday, July 30, 2008

Part of a project I’m working on requires users to tag certain items.  These tags are subscribed to by other systems and are also used to provide some neat auto-complete features for other inputs throughout the site.  Since new tags can be added by a user, I want to make sure that when the tag is added, the capitalization is in Title Case.  Here’s a quick demonstration of how to use mbUnit’s RowTest feature to allow you to test more scenarios with less code.  First, lets take a standard, single scenario test:

As you can see above, I am setting an expectation on my repository that when it is called, it will insert the original input as a properly formatted string.  The formatting will be implemented within my service class.  Running this yielded a filed test – RED (good!). 

Now let’s implement this feature:

I decided to create a string extension method which uses the little known ToTitleCase method of the TextInfo class.  This method accepts a string and returns it as a title-cased string.  Also note how we can split the dot-notation across lines making it much cleaner.  Below is the actual service call which will format the string and call my IRepository method:


After implementing this, I went back to the test and ran it.  Success!

At this point, something occurred to me.  The tag name “test tag” is easy to verify.  However, how will this implementation handle capitalization rules?  For example, if I enter “LCD Repair” as a tag, it should NOT format it to “Lcd Repair”.  This is where the RowTest feature shines.  Instead of writing a new test for each of these scenarios, I just converted my “Test” attribute to “RowTest” and changed the method signature.

As we can see, the method signature matches the individual Row parameters.  When we run this test again, it executes the test once for each row.  When you actually look at the test runner, it shows up as 3 individual tests so each row can fail independently.

Hope this helps!


  1. Michael Hall says:

    I love RowTests. Although I use NUnit + the NUnit RowTest Extension For NUnit ( The reason I’m not using MbUnit is because of the “That” method, it just feels more right. Aside from that the only difference is adding a “bool expectedPass” parameter to the method so I can combine logical pass or failure tests within the same method. So like what you’re doing, only they will all turn green. Instead of Assert.IsTrue(result.Success) it would be something like Assert.That(result.IsTrue, expectedPass). Obviously this really only works for simple conditions but it’s easier than having to maintain (Happy|Sad) Path tests for everything.

    (BTW I was linked through from “A Continous Learner’s” blog, that makes two times I blind clicked onto your blog. Good stuff!)

  2. JamesDX says:

    Anyone know how to do things like this?

Leave a Reply