Available for hire
Date & Time Helper With Rspec and Capybara

A set of simple helpers for you to place into ./spec/helpers/helpers.rb, I searched for a while and couldn’t find an elegent solution, so I quickly whiped this up and thought I’d share incase anyone else needed a hand.

The first helps select a date from a date and time rails widget

1
2
3
4
5
6
7
8
def select_date_and_time(date, options = {})
  field = options[:from]
  select date.strftime('%Y'), :from => "#{field}_1i" #year
  select date.strftime('%B'), :from => "#{field}_2i" #month
  select date.strftime('%d'), :from => "#{field}_3i" #day 
  select date.strftime('%H'), :from => "#{field}_4i" #hour
  select date.strftime('%M'), :from => "#{field}_5i" #minute
end

To use it, in your capybara rpsec tests, you can find use it by

1
select_date_and_time(DateTime.now, from:published_date)

Similarly the following helper selects just a date

1
2
3
4
5
6
def select_date(date, options = {})
  field = options[:from]
  select date.strftime('%Y'), :from => "#{field}_1i" #year
  select date.strftime('%B'), :from => "#{field}_2i" #month
  select date.strftime('%d'), :from => "#{field}_3i" #day 
end

And use it as follows in your tests

1
select_date(Date.today, from:published_date)

Comments