title: How to properly mock private members of a class
body: <p>I am trying to write some unit tests for a method that depends on another private method. - As shown in the example below:</p>
  
  <pre><code>def is_member_of(self, group_name):
    members = self.__get_group_members(group_name)
  </code></pre>
  
  <p>The private method that I'd like to mock is <code>__get_group_members</code>; I'd also like to mock the private attribute <code>__user_id</code> since it will be used in the <code>is_member_of</code> function (not shown in the example above).</p>
  
  <p>What I have so far:</p>
  
  <pre><code>import unittest
  from unittest import mock
  
  class Test(unittest.TestCase):
    group_data = []
    user_id = 'test_user_id'
  
    def mock_dependencies(self, x):
      x.__user_id = mock.PropertyMock(return_value=self.user_id)
      x.__get_group_members = mock.MagicMock(return_value=self.group_data)
  
    def first_test(self):
      x = A(('name', 'group'))
      self.mock_dependencies(x)
      x.is_member_of('test_group')
  </code></pre>
  
  <p>When I invoke <code>x.is_member_of()</code> the mocking doesn't work as anticipated. </p>