Saturday, September 18, 2010

FitNesse and today's date with .net

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:

Given Mary who is born January 5, 1995 and lives in Manitoba
When she asks if she is the age of majority
Then return no

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)

!|Check Age of Majority|
|Province State|Birth Date|Am I Underage?|
|MB            |5-Jan-1995|Yes           |

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:

namespace FitNesseTutorial.Tests
      public class GetDateBasedOnToday : ColumnFixture
        public int AddYears;
        public int AddDays;

        public DateTime ResultingDate()
            return DateTime.Today.AddYears(AddYears).AddDays(AddDays);

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:

The FitNesse script:

''Get underage and of age dates for 18 and 19 year olds''
!|Get Date Based On Today          |
|Add Years|Add Days|Resulting Date?|
|-18      |1       |>>UNDERAGE_18  |
|-19      |1       |>>UNDERAGE_19  |
|-18      |0       |>>OFAGE_18     |
|-19      |0       |>>OFAGE_19     |

!|Check Age of Majority|
|Province State|Birth Date   |Am I Underage?|
|MB            |<<OFAGE_18   |Yes           |
|MB            |<<UNDERAGE_18|No            |
|BC            |<<OFAGE_19   |Yes           |
|BC            |<<UNDERAGE_19|No            |

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.)