Taking Rules to Court

time to read 8 min | 1483 words

It's about time I'll post about my trouble making rules again. I've been doing some DATT* work on them, and I got to the point where I can write a rule in an hour or so (including tests, of course), instead of a day.

Partly it is because I'm now using Fitnesse to write the tests, which allows for a very nice way to declare rules and various states and then test them. I'm not sure if the problem is with what I'm doing or the way I'm doing it, but when I wrote the tests in C# I got a lot of tedious initialization code, one method call, and a lot of verfication code. Using Fitnesse I'm able to express that in a natural syntax:

create rules
create employee ayende
add employee rule coffee breaks every 3 hours

coffe break
employee coffee break time
ayende 13:00
ayende 14:00
ayende 17:00
ayende 20:00


validate employee ayende
can't take a coffee break on 14:00, take another on 16:00
what are you doing working on 20:00, go home!

It takes about ten minutes to write that, including the code that makes it run, and it's very malleble to change, so I can very easily create new test cases, or modify existing one. Fitnesse is usually used for acceptance tests, and I can certainly see why. The problem in testing rules is that almost by defination, they are first and foremost business concerns, and they usually need something to run on, which mean a lot of state to setup.

There are some patterns about dealing with that, but I found that Fitnesse is easy and add a lot to the fun. This week the FitLibrary for .Net should be out, which will make it even better.

* Design All The Time