title: How to mock data as request.Response type in python
body: <p>I would like to write some testcase to exercise <em>object_check</em> in isinstance(obj, requests.Response) logic. After I create Mock data as return value for requests.post. The type for mock data is always be Mock class. In that way, how can I rewrite mock data so mock data can be type of requests.Response? so I can exercise line <code>d = obj.json()</code>?</p>
  
  <pre><code>from unittest.mock import patch, Mock
  import unittest
  import requests
  from requests.exceptions import HTTPError
  import pytest
  def object_check(obj):
      if isinstance(obj, bytes):
          d = ujson.decode(obj.decode())
      elif isinstance(obj, requests.Response):
          d = obj.json()
      else:
          raise ValueError('invalid type')
      return d
  
  def service_post(params):
      """
      trivial function that does a GET request
      against google, checks the status of the
      result and returns the raw content
      """
      url = "https://www.iamdomain.com"
      params = {'number': 1234, 'user_id': 1, 'name': 'john'}
      resp = requests.post(url, data=params)
      return object_check(resp)
  
  @patch.object(requests, 'post')
  def test_service_post(mock_request_post):
      data = {'number': 0000, 'user_id': 0, 'name': 'john'}
      def res():
          r = Mock()
          r.status_code.return_value = 200
          r.json.return_value = data
          return r
      mock_request_post.return_value = res()
      assert data == service_post(data)
  </code></pre>