title: Mocking assert_called_with in Python
body: <p>I'm having some trouble understanding why the following code does not pass:</p>
  
  <p><strong><em>test.py</em></strong></p>
  
  <pre><code>import mock
  import unittest
  
  from foo import Foo
  
  
  class TestFoo(unittest.TestCase):
      @mock.patch('foo.Bar')
      def test_foo_add(self, Bar):
          foo = Foo()
          foo.add(2, 2)
          Bar.add.assert_called_with(2, 2)
  
  
  if __name__ == '__main__':
      unittest.main()
  </code></pre>
  
  <p><strong><em>foo.py</em></strong></p>
  
  <pre><code>from bar import Bar
  
  class Foo(object):
      def add(self, x, y):
          bar = Bar()
          return bar.add(x, y)
  </code></pre>
  
  <p><strong><em>bar.py</em></strong></p>
  
  <pre><code>class Bar(object):
      def add(self, x, y):
          print('b.Bar --&gt; Adding {} + {}'.format(x, y))
          return x + y
  </code></pre>
  
  <p>In the code, <code>Foo.add</code> creates an instance of <code>Bar</code> and returns the result of <code>Bar.add</code> when invoked. Why does testing <code>assert_called_with</code> for <code>Bar.add</code> fail? I believe I am mocking the <code>Bar</code> in the correct place (I am mocking <code>foo.Bar</code> because that is the namespace which it is being looked up, rather than <code>bar.Bar</code>).</p>
  
  <p>Traceback (most recent call last):
    File "/Users/iain/PycharmProjects/testing/venv/lib/python2.7/site-packages/mock.py", line 1201, in patched
      return func(*args, **keywargs)
    File "test.py", line 12, in test_a_b
      fake_Bar.add.assert_called_with(2, 2)
    File "/Users/iain/PycharmProjects/testing/venv/lib/python2.7/site-packages/mock.py", line 831, in assert_called_with
      raise AssertionError('Expected call: %s\nNot called' % (expected,))
  AssertionError: Expected call: add(2, 2)
  Not called</p>