-
-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: Mock current date/time #81
Comments
Ah, that won't work because we cannot replace/patch these built-ins when they're read-only. What you do typically is to replace the datetime object with a "spy". Basically, as you can't patch Unfortunately I'm not familiar with the test framework/layout you showed here in |
Thanks for the answer. I was looking for a way to accomplish this without having to modify the function I was unit testing to accept an injected
|
Injecting was only one variant - basically the old school variant. You ignored The code you provided as the example is self-contained, and that makes everything Here is a sketch of how it should work: # module_under_test.py
from datetime import datetime
def what_time_is_it():
return datetime.now()
# test.py
from datetime import datetime
import module_under_test
with description('current date') as self:
with before.each:
fake_datetime = spy(datetime)
when(fake_datetime).now().thenReturn("NOW")
module_under_test.datetime = fake_datetime # <== you patch the module
with after.each:
module_under_test.datetime = datetime
with it('can mock the date'):
assert module_under_test.what_time_is_it() == "NOW" But if you only use with description('current date') as self:
with before.each:
module_under_test.datetime = mock({"now": lambda: "NOW"})
with after.each:
module_under_test.datetime = datetime
with it('can mock the date'):
assert module_under_test.what_time_is_it() == "NOW" You could also imagine that you deep import # module_under_test.py
from datetime.datetime import now
def what_time_is_it():
return now()
# test.py
from datetime import datetime
import module_under_test
with description('current date') as self:
with before.each:
when(module_under_test).now().thenReturn("NOW")
with after.each:
unstub()
with it('can mock the date'):
assert module_under_test.what_time_is_it() == "NOW" So there is some design space before choosing the freezegun. |
Interesting. Thank you for taking the time to make these examples! I was doing what you were doing in the third example previous to this. But, I didn't like having that extra function that just returned the date. I never though of doing it the way you are in the first example, though. |
Add support for mocking the value returned from functions like
datetime.datetime.now()
. I have several functions that make use of the current date. I have not found a way to appropriately test those using Mockito.The example below is how I have tried to mock the current date/time. I am not asking date/time freezing to work exactly like this example. This is just to help show what I am talking about.
Example
requirements.txt
datetime_spec.py
Command
Error Message
The text was updated successfully, but these errors were encountered: