Skip to content

Instantly share code, notes, and snippets.

@lukmdo
Created January 16, 2013 12:44
Show Gist options
  • Save lukmdo/4546883 to your computer and use it in GitHub Desktop.
Save lukmdo/4546883 to your computer and use it in GitHub Desktop.
dummy datetime mocking (Better use a DUMMY class the is cmp/eq True always)
import unittest2 as unittest
from mock import patch
from mock import Mock
from datetime import datetime as dt
class TestMock(unittest.TestCase):
NOW = datetime.datetime(1970, 1, 1, 0, 0)
datetime_mock = Mock(side_effect = lambda *args, **kw:
dt(*args, **kw))
datetime_mock.now.return_value = NOW
def setUp(self):
super(TestMock, self).setUp()
self.datetime_mock.reset_mock()
@patch('datetime.datetime', new=datetime_mock)
def test_foo(self):
self.assertEqual(
datetime.datetime(1970, 1, 1, 0, 0),
datetime.datetime.now())
self.assertEqual(1, self.datetime_mock.call_count)
@patch('datetime.datetime', new=datetime_mock)
def test_bar(self):
self.assertEqual(
datetime.datetime(1970, 1, 1, 0, 0),
datetime.datetime.now())
self.assertEqual(1, self.datetime_mock.call_count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment