Created
June 20, 2016 12:34
-
-
Save goncalvesjoao/7d1bcb5c3737da0e80dceb48a53efa7f to your computer and use it in GitHub Desktop.
date time abstraction that enables tests to manipulate the current date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module DT | |
extend self | |
DEFAULT_TIME_ZONE = 'Lisbon' | |
def now | |
(mock_data(:now) || DateTime.now).utc | |
end | |
def datetime_for_query(date, time_zone = DEFAULT_TIME_ZONE) | |
I18n.l(date.in_time_zone(time_zone), format: '%Y-%m-%d %H:%M:%S.%6N') | |
end | |
def set_mock(key, data) | |
mocks[key] = data | |
end | |
protected ######################## PROTECTED ########################### | |
def mocks | |
@mocks ||= {} | |
end | |
def mock_data(key) | |
return nil if ENV['ENV'] != 'test' | |
mocks[key] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment