Faker: Take into account test duration when generating random dates

Created on 29 Sep 2018  路  2Comments  路  Source: faker-ruby/faker

Often I'm running tests without freezing time, and I need to generate a "time between" Time.now and some arbitrary time in the future, and make sure that the randomly selected time will not become a past time given the time of execution of the test (otherwise the test will be flaky).

FOr instance

Utility.some_range_between(Time.now, end_time) to always generate a time that would be future? at any point during my test (since my app code will actually compare such a date with Time.now to decide on some behavior)

Therefore I have selected an arbitrary test_max_duration (15.seconds for instance) and I have enhanced my own utility methods to return a random range to include this safety margin

def Utility.some_range_between(start_time, end_time)
  (start_time + TEST_MAXIMUM_DURATION)..(end_time - TEST_MAXIMUM_DURATION)
end

I'm wondering if this could be integrated into faker (maybe with some warning if the coder tries to request a random date within the TEST_MAXIMUM_DURATION to indicate it will be required to freeze time to avoid problems)

Question

Most helpful comment

It seems to me like you should be freezing time in your tests if your tests are time-sensitive, rather than pushing that concern down to Faker. Let me know if you think there are other use-cases for the functionality, though.

All 2 comments

It seems to me like you should be freezing time in your tests if your tests are time-sensitive, rather than pushing that concern down to Faker. Let me know if you think there are other use-cases for the functionality, though.

https://rubygems.org/gems/timecop is a good tool for testing with time, though some testing frameworks may get upset at it, so just use it where it's needed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vnbrs picture vnbrs  路  5Comments

zyrthofar picture zyrthofar  路  6Comments

supiash1 picture supiash1  路  5Comments

kevgathuku picture kevgathuku  路  4Comments

vbrazo picture vbrazo  路  3Comments