I have an acceptance test that says I need to validate the age of majority in each of the different states and provinces. Here is a simple example:
The test above is faily simple and I could write it like this in the wiki as a Column Fixture (using the fitSharp.dll to test C# code)
The problem of course is that this test will start failing on January 5, 2013 when Mary turns 18. Also, it does not perform the boundary testing that I would like it to do in order to test someone who is 18 today vs. someone who will turn 18 tomorrow. In order to improve this test, I investigated some other date functions in FitNesse and a plugin by James Carr that allowed you to add days to the current date. These work ok for smaller calculations like “Given document ABC, When it is 30 days old, Then archive it”. However, this would be a little more cumbersome for birth dates when adding 18 years (esp. with leap year calculations) and the !today function in FitNesse does not work in ColumnFixture wiki tables. So, I found a simple way to meet my requirement.
First, I wrote a class in C# that accepts two parameters to Add or Subtract Years and Days to the current date. The class uses C#’s simple DateTime addition to add or subtract the years/days from today and returns the result. You could easily extend this to add months or add other functionality required in your tests:
Then in FitNesse at the top of my script for this story I call GetDateBasedOnToday and store the resulting values in FitNesse variables. Finally, I use the variable names through my script to reference the underage and of age birth dates. Here is an example:
In FitNesse, the final result including the acceptance criteria above looks like this:
(Note: The example above should probably be written as a unit test because it is fairly straightforward, but it simply illustrates how to use the date logic that I’m using as part of larger acceptance tests.)
Re-posted from http://winnipegagilist.blogspot.com