I'm trying to use the Python mock module (downloaded using pip) for the first time. I'm having problems setting an assert, I've narrowed it down to this code:
class TestUsingMock(unittest.TestCase):
def setUp(self):
self.fake_client = mock.Mock()
def test_mock(self):
self.fake_client.copy = mock.Mock()
self.fake_client.copy("123")
self.fake_client.assert_called_with("123")
if __name__ == "__main__":
unittest.main()
This is the error I get:
F
======================================================================
FAIL: test_mock (__main__.TestVCSDriver)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./mock_test.py", line 17, in test_mock
self.fake_client.assert_called_with("123")
File "/Library/Python/2.6/site-packages/mock.py", line 859, in assert_called_with
raise AssertionError('Expected call: %s\nNot called' % (expected,))
AssertionError: Expected call: mock('123')
Not called
Without the assertion, everything works fine. What am I doing wrong?
asked Jun 1, 2012 at 8:59
seanhodges
17.6k17 gold badges75 silver badges95 bronze badges
1 Answer 1
You are calling the object self.fake_client.copy, but test whether another one, self.fake_client has been called.
Either call the "correct" object:
self.fake_client("123")
self.fake_client.assert_called_with("123")
or test the copy:
self.fake_client.copy("123")
self.fake_client.copy.assert_called_with("123")
answered Jun 1, 2012 at 9:50
Ferdinand Beyer
67.7k18 gold badges161 silver badges147 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
seanhodges
Your second example was what I intended, thanks for your help!
lang-py