Mocker.replace issue

Asked by Nathen Hinson

When run through unittest Mocker doesn't seem to be removing its replaced object.

Examples follow:

The expected behavior works in an interpreter, such as:

>>> from mocker import Mocker,KWARGS
>>> from datetime import datetime
>>> m = Mocker()
>>> m_dt = m.replace(datetime)
>>> m_dt.now(KWARGS)
<mocker.Mock object at 0xb7c1280c>
>>> m.result('Hi')
>>> m.count(0,None)
>>> m.replay()
>>> datetime.now()
'Hi'
>>> m.reset()
>>> datetime.now()
datetime.datetime(2009, 2, 13, 10, 35, 39, 233562)

But when run inside a setUp tearDown unittest framework the reset fails to reset the 'mocked' object back.

Example code:

from datetime import datetime
import unittest
from mocker import Mocker

class TestDateTime(unittest.TestCase):

    def setUp(self):
        self.mocker = Mocker()
        mock_datetime = self.mocker.replace(datetime)
        mock_datetime.now()
        self.mocker.result('Hi')
        self.mocker.count(0,None)
        self.mocker.replay()

    def tearDown(self):
        self.mocker.reset()

    def test_mock_DateTime(self):
        """ Testing the mock, which succeeds """
        self.assertEqual('Hi',datetime.now())

    def test_real_DateTime(self):
        """ Testing the "un-mocking" of datetime,
            which fails.
        """
        self.assertNotEqual('Hi',datetime.now())

if __name__ == '__main__':
    unittest.main()

Running this produces the following output:

.F
=====================================
FAIL: test_real_DateTime (__main__.TestDateTime)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "mock_replace_example.py", line 24, in test_real_DateTime
    self.assertNotEqual('Hi',datetime.now())
AssertionError: 'Hi' == 'Hi'

----------------------------------------------------------------------
Ran 2 tests in 0.003s

FAILED (failures=1)

Any assistance would be appreciated.

Question information

Language:
English Edit question
Status:
Solved
For:
Mocker Edit question
Assignee:
No assignee Edit question
Solved by:
Nathen Hinson
Solved:
Last query:
Last reply:
Revision history for this message
Nathen Hinson (nathen-hinson) said :
#1

My example was incorrect. After correcting it, my example issue went away. I'm going back to the drawing board.