3

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

1 Answer 1

8

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
Sign up to request clarification or add additional context in comments.

1 Comment

Your second example was what I intended, thanks for your help!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.