Make Your Cucumber Step Definitions Time Aware

If you’re like me, you’ve found yourself with a cucumber step definition like this:

Given /^I recieved an invitation$/ do  
  # ...
end  

And you want to write a step definition like this:

Given /^I recieved an invitation 2 days ago$/ do  
  # ...
end  

Instead of doing all that extra work, I threw together a cucumber step that lets you add times to any existing step definition:

Given /^(.+) (\d+) (seconds?|minutes?|hours?|days?|months?|years?) (ago|from now)$/ do |string, number, time_unit, time_direction|  
  Timecop.freeze(number.to_i.send(time_unit).send(time_direction.gsub(' ','_'))) do
    Given string
  end
end  

To make things even easier I created a gem called Timebomb. It lets you append time constraints to your cucumber step defintions by mixing and matching:

  • seconds, minutes, hours, days, weeks, months, years
  • ago, from now

So with the following step definition:

Given /^I recieved an invitation$/ do  
  # ...
end  

I can write any of the following:

  • Given I received an invitation 1 day from now
  • Given I received an invitation 2 weeks ago
  • Given I received an invitation 3 months from now
  • Given I received an invitation 15 years ago

Timebomb to the rescue, BOOM!

Installing

First install Timebomb

gem install timebomb  

Now, just require it in your cucumber env.rb file. BOOM goes the dynamite.