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!