title: What's wrong with this Python mock patch?
body: <p>I'm having trouble mocking out the an imported module in a unit test. I'm trying to mock the PIL Image class in my module <code>tracker.models</code> using the mock module. I understand you are supposed to mock things where they are used, so I've written <code>@mock.patch('tracker.models.Image')</code> as my decorator for the unit test. I am trying to check whether the downloaded image gets opened as a PIL Image. The mock patch seems to be overwriting the entire Image module. Here is the error I'm getting when I run the test:</p>
  
  <pre><code>File "/home/ubuntu/workspace/tracker/models.py", line 40, in set_photo
      width, height = image.size
  ValueError: need more than 0 values to unpack
  </code></pre>
  
  <p>Here's my unit test:</p>
  
  <p><strong>test_models.py</strong></p>
  
  <pre><code>@responses.activate
  @mock.patch('tracker.models.Image')
  def test_set_photo(self, mock_pil_image):
      # Initialize data
      hammer = Product.objects.get(name="Hammer")
      fake_url = 'http://www.example.com/prod.jpeg'
      fake_destination = 'Hammer.jpeg'
  
      # Mock successful image download using sample image. (This works fine)
      with open('tracker/tests/test_data/small_pic.jpeg', 'r') as pic:
          sample_pic_content = pic.read()
      responses.add(responses.GET, fake_url, body=sample_pic_content, status=200, content_type='image/jpeg')
  
      # Run the actual method
      hammer.set_photo(fake_url, fake_destination)
  
      # Check that it was opened as a PIL Image
      self.assertTrue(mock_pil_image.open.called,
                      "Failed to open the downloaded file as a PIL image.")
  </code></pre>
  
  <p>Here is the piece of code it is testing.</p>
  
  <p><strong>tracker/models.py</strong></p>
  
  <pre><code>class Product(models.Model):
      def set_photo(self, url, filename):
          image_request_result = requests.get(url)
          image_request_result.content
          image = Image.open(StringIO(image_request_result.content))
  
          # Shrink photo if needed
          width, height = image.size  # Unit test fails here
          max_size = [MAX_IMAGE_SIZE, MAX_IMAGE_SIZE]
          if width &gt; MAX_IMAGE_SIZE or height &gt; MAX_IMAGE_SIZE:
              image.thumbnail(max_size)
          image_io = StringIO()
          image.save(image_io, format='JPEG')
          self.photo.save(filename, ContentFile(image_io.getvalue()))
  </code></pre>