[rspec-users] [cucumber] Cucumber and Chronic

Tim Walker walketim at gmail.com
Sun Mar 1 11:50:37 EST 2009


This may have been discussed before but just wanted to pass along a
killer one-two punch that we stumbled upon as an excellent and natural
extension to our domain language around the expression and testing of
complex time based constructs.

Please consider:

Cucumber allows you to specify behavior in natural language.

Chronic is a natural language date/time parser written in pure Ruby.
(http://chronic.rubyforge.org/)

Example of a cucumber test:

Scenario: Test control operations
Given that I have have a control operation named “set clock date time”
to “Monday Jan 1, 2009 10:00”
When the control named “set clock date time” runs
Then the clock should be “10:00”

Now, this works fine, if you’re only testing that you can set the
clock to "Monday Jan 1, 2009 10:00". It gets more complex quickly when
you try to do much more than this. Adding chronic to the mix allows us
to define and use more complex time concepts in plain language. This
allows for the parsing of sentence fragments such as “next month” and
“next Tuesday” and combine them in to complex structures such as “next
Tuesday at 9:00” in a way that our tests continue to run as time
marches on.

For example:
>> sentence = "next tuesday at 3:00 pm"
=> "next tuesday at 3:00 pm"
>> time = Chronic.parse(sentence)
=> Tue Mar 03 15:00:00 -0700 2009

Now, the scenario can use this easily express clearly “when” the
control operation runs in more complex and useful expressions, for
example:

Scenario: Test control operations
Given that I have have a control operation named “set clock date time”
to "next tuesday at 3:00 pm"
When the control named “set clock date time” runs
Then the clock should be “next tuesday at 3:00 pm"

Scenario: Test spy sweeping schedules
Given that I have have a control operation named “clean spies”  that
is scheduled to run “next Monday at 8:00 AM"
And the customer has excluded “weekdays” from allowing the control
operation “clean spies” to run
When the control named “set clock time” runs
Then it should log a message and quit


More information about the rspec-users mailing list