title: Mocking a property call returning MagicMock, not value
body: <p>I have the following configuration class:</p>
  
  <pre><code>class ConfigB(object):
    Id = None
  
    def __Init__(self, Id):
      self.Id = Id
  </code></pre>
  
  <p>Which is instantiated in the following class and the property printed:</p>
  
  <pre><code>from config.ConfigB import ConfigB
  
  class FileRunner(object):
    def runProcess(self, Id)
      cfgB = ConfigB(Id)
      print(cfgB.Id)
  </code></pre>
  
  <p>I have created the following test class to test it, where I am trying to mock both the instantiation and the cfgB.Id property call:</p>
  
  <pre><code>import unittest
  import unittest.mock imort MagicMock
  import mock
  from FileRunner import FileRunner
  
  class TestFileRunner(unittest.TestCase):
    @mock.patch('ConfigB.ConfigB.__init__')
    @mock.patch('ConfigB.ConfigB.Id')
    def test_methodscalled(self, cfgBId, cfgBInit):
  
  
      fileRunner = FileRunner()
  
      cfgBId.return_value = 17
  
      cfgBInit.return_value = None
  
      print(cfgBId)
  
      fileRunner.runProcess(1)
  </code></pre>
  
  <p>Note the print(cfgBId) statement before fileRunner is called. I get the following output:</p>
  
  <pre><code>&lt;MagicMock name='Id' id='157297352'&gt;
  &lt;MagicMock name='Id' id='157297352'&gt;
  </code></pre>
  
  <p>For some reason when I set the return value here in the test class: </p>
  
  <pre><code>cfgBId.return_value = 17
  </code></pre>
  
  <p>That is not getting called on the line in the FileRunner() class:</p>
  
  <pre><code>print(cfgB.Id)
  </code></pre>
  
  <p>What do I need to do to properly get my configuration property to display? </p>
  
  <p>Also note that my 'ConfigB' class instantiation is much more complicated than displayed above which is why I want to patch the instantiation and the call to the Id property.</p>
  
  <p>*Update: I have changed my class as suggested by @mgilson but it is still not working:</p>
  
  <pre><code>import unittest
  import unittest.mock imort MagicMock
  import mock
  from FileRunner import FileRunner
  
  class TestFileRunner(unittest.TestCase):
    @mock.patch('FileRunner.ConfigB')
    def test_methodscalled(self, cfgB):
  
      fileRunner = FileRunner()
  
      cfgB.Id = 17
  
      print(cfgBId)
  
      fileRunner.runProcess(1)
  </code></pre>
  
  <p>I am now getting the following output from the two print statements:</p>
  
  <pre><code>&lt;MagicMock name='ConfigB' id='157793640'&gt;
  &lt;MagicMock name='ConfigB().Id' id='157020512'&gt;
  </code></pre>
  
  <p>Any ideas why the above isn't working?</p>
  
  <p>*Solution:</p>
  
  <p>I was able to get it to work after a minor change to the test method that @mgilson suggested:</p>
  
  <pre><code>import unittest
  from unittest.mock import MagicMock
  import mock
  from FileRunner import FileRunner
  
  class TestFileRunner(unittest.TestCase):
    @mock.patch('FileRunner.ConfigB')
    def test_methodscalled(self, configB):
  
      fileRunner = FileRunner()
  
      cfgB = MagicMock()
      cfgB.Id = 17
      #This will return the cfgB MagicMock when `ConfigB(Id)` is called in `FileRunner` class
      configB.return_value = cfgB 
  
      print(cfgB.Id)
  
      fileRunner.runProcess(1)
  
      #To test whether `ConfigB(17)` was called
      configB.assert_called_with(17)
  </code></pre>
  
  <p>I now get the following outputs:</p>
  
  <pre><code>&lt;MagicMock name='ConfigB' id='157147936'&gt;
  17
  </code></pre>