So what you can do if you have to mock this kind of methods in your tests?
One possible option is to use the forbiddenfruit library (https://github.com/clarete/forbiddenfruit).
This project aims to help you reach heaven while writing tests, but it may lead you to hell if used on production code.Here you can find a plone.app.testing layer definition that helps you to patch the datetime.datetime.now method:
It basically allows you to patch built-in objects, declared in C through python.
class FakeDateTime(PloneSandboxLayer):Links:
defaultBases = (PLONE_FIXTURE,)
def setUp(self):
import datetime
original_now = datetime.datetime.now
first_now = original_now()
delta = datetime.datetime(first_now.year + 1, 1, 1, 0, 0, 0) - first_now
def fake_now(self):
now = original_now()
return now + delta
curse(datetime.datetime, 'now', classmethod(fake_now))
def tearDown(self):
import datetime
from forbiddenfruit import reverse
reverse(datetime.datetime, 'now')
- https://github.com/clarete/forbiddenfruit
- mocking datetime with mock via https://twitter.com/timostollenwerk/status/398381575995273216
No comments:
Post a Comment
Note: only a member of this blog may post a comment.