title: Mocking requests.post and requests.json decoder python
body: <p>I'm creating a test suite for my module that uses the requests library quite a bit. However, I'm trying to mock several different return values for a specific request, and I'm having trouble doing so. Here is my code snippet that doesn't work:</p>
  
  <pre><code>class MyTests(unittest.TestCase):
  
      @patch('mypackage.mymodule.requests.post') 
      def test_change_nested_dict_function(self, mock_post):
          mock_post.return_value.status_code = 200
          mock_post.return_value.json = nested_dictionary
          modified_dict = mymodule.change_nested_dict()
          self.assertEqual(modified_dict['key1']['key2'][0]['key3'], 'replaced_value')
  </code></pre>
  
  <p>The function I am attempting to mock:</p>
  
  <pre><code>import requests
  
  def change_nested_dict():
      uri = 'http://this_is_the_endpoint/I/am/hitting'
      payload = {'param1': 'foo', 'param2': 'bar'}
      r = requests.post(uri, params=payload)
  
      # This function checks to make sure the response is giving the 
      # correct status code, hence why I need to mock the status code above
      raise_error_if_bad_status_code(r)
  
      dict_to_be_changed = r.json()
  
      def _internal_fxn_to_change_nested_value(dict):
          ''' This goes through the dict and finds the correct key to change the value. 
              This is the actual function I am trying to test above'''
          return changed_dict
  
  
      modified_dict = _internal_fxn_to_change_nested_value(dict_to_be_changed)
  
      return modified_dict
  </code></pre>
  
  <p>I know a simple way of doing this would be to not have a nested function, but I am only showing you part of the entire function's code. Trust me, the nested function is necessary and I really do not want to change that part of it.</p>
  
  <p>My issue is, I don't understand how to mock requests.post and then set the return value for both the status code and the internal json decoder. I also can't seem to find a way around this issue since I can't seem to patch the internal function either, which also would solve this problem. Does anyone have any suggestions/ideas? Thanks a bunch.</p>