Tom Tom - 4 months ago 13
Ruby Question

Cucumber Date Ranges Ruby

I've seen some similar questions on here regarding date ranges yet none of the solutions seem to work for me. What i'm trying to do is have a date range of a month and confirm if today's date is within that range. Eventually, this will be put into a case method for every month of the year as the functionality I'm testing is date specific.

I tried to convert the dates to integer to make the calculation easier (or so I thought) then use

between?
to check the date range.

This is my code:

today = Time.now.to_i

month_start = Time.parse('1 Jan 2016').to_i

month_end = Time.parse('31 Jan 2016').to_i

if today.between?(month_start,month_end)
#do something
end


When having a puts on each variable, this is the output:

Today = 1468479863

month_start = 1451606400

month_end = 1454198400


As you can see, this should fail as today is not between the date range, it's far outside it. Yet, the tests are going green which would suggest my
if
statement containing the
between?
method isn't working.

Is there something blindingly obvious that I'm missing here as I can't see it.

The step in the feature is here:

Then(/^I can see that all results have a statement due this month$/) do
today = Time.now.to_i

month_start = Time.parse('1 Jan 2016').to_i

month_end = Time.parse('31 Jan 2016').to_i

results_table = all('table#clickable-rows tbody tr')

if today.between?(month_start,month_end)
results_table.each do |row|
within(row.all('td')[3]) do
statement_date = find('table#clickable-rows tbody tr td:nth-child(4) > span')
expect(statement_date).to have_text '1 Aug 2016'
end
end
end
end

Answer

Your tests pass, because if today.between?(month_start,month_end) return false, no expectations are run!

You might want to consider changing the if into an expectation in itself:

Then(/^I can see that all results have a statement due this month$/) do
    today = Time.now.to_i

    month_start = Time.parse('1 Jan 2016').to_i

    month_end = Time.parse('31 Jan 2016').to_i

    results_table = all('table#clickable-rows tbody tr')

    expect(today).to be_between(month_start,month_end)
    results_table.each do |row|
      within(row.all('td')[3]) do
        statement_date = find('table#clickable-rows tbody tr td:nth-child(4) > span')
        expect(statement_date).to have_text '1 Aug 2016'
      end
    end
  end
Comments