title: Mocking attributes using @patch decorator
body: <p>I am struggling with mocking attributes in my Python tests. The function I am trying to test keeps failing because the mock probably returns the right value but is the wrong type (Should be string and it is a MagicMock instead.</p>
  
  <p>I have found this answer and I understand I need to use a PropertyMock. But I can't get it to work neither with the context manager or using the @patch decorator. <a href="https://stackoverflow.com/questions/16867509/mock-attributes-in-python-mock">Mock attributes in Python mock?</a></p>
  
  <p>Here is my test:</p>
  
  <pre><code> @patch('keys.views.requests.post')
   @patch('keys.views.requests.Response.text', new_callable=PropertyMock)
   def test_shows_message_when_receives_error(self, mock_response_text     ,mock_post):
  
       expected_error = escape(MESSAGE)
       data_to_be_received = json.dumps({
           "message":"Bad Request",
           "errors":[{
               "resource":"Application",
               "field":"client_id",
               "code":"invalid"
           }]
       })
       mock_response_text.return_value = data_to_be_received
  
       response = self.client.get('/users/tokenexchange?state=&amp;code=abc123')
  
       self.assertContains(response, expected_error)
  </code></pre>
  
  <p>And the code I am testing:</p>
  
  <pre><code>def token_exchange(request):
      parameters = {'client_id': '##', 'client_secret': '##', 'code': code}
  
      response = requests.post('https://www.strava.com/oauth/token', parameters)
      data_received = json.loads(response.text)
  
      if 'errors' not in data_received:
          return HttpResponse(response.text)
       else:
           return render(request, 'home.html', {'error': STRAVA_AUTH_ERROR})
  </code></pre>
  
  <p>The error I keep getting is:</p>
  
  <pre><code>File "##", line 66, in token_exchange
      data_received = json.loads(response.text)
  
  TypeError: the JSON object must be str, bytes or bytearray, not 'MagicMock'
  </code></pre>
  
  <p>Thanks for your answers!!!</p>