I have some tests which depends on current time and am not able to find a solution for it so far. I have tried some mocking libraries like mock
but it mocks the whole module and it fails.
Any help will be really appreciated(if I only mock DateTime.utc_now
everything is ok)
Note: tests depends heavily on other DateTime
and Date
functions so mocking whole modules is not a very good option(I have tried this also but failed due to very complex cases and I need this in many tests)
Actual test:
I have two dates, start date
and end date
as input to a function which I am trying to test. Before calling the function for test purpose I insert some data relevent to the current week(current dat to next seven days). Now the function will get current datetime and check for specific days(each record will tell if it applies to current day of the week and for current time period range on which being iterated -> start and end dates).
e.g one record applies for mon
-> 2:12 to 3:13
The solution which best suits my needs(simple, works well and according to the requirements described above) is:
define your own function/service
MyDateTime.utc_now/0
and mock it in your tests. — Reference.