title: How to patch Python class using Mock library
body: <p>I'm having trouble patching a class for my testing. I'm trying to patch something that belongs to Scrapy - a <a href="http://scrapy.readthedocs.org/en/latest/topics/selectors.html?highlight=HtmlXPath#scrapy.selector.HtmlXPathSelector" rel="nofollow">HtmlXpathSelector</a> class.</p>
  
  <p>Here is some code:</p>
  
  <pre><code>from scrapy.selector import HtmlXPathSelector
  from mock import MagicMock, patch
  
  with patch('scrapy.selector.HtmlXPathSelector') as MockHtml:
      instance = MockHtml.return_value
      instance.method.return_value = 'foo'
      example = HtmlXPathSelector()
      print type(example)
      assert example is instance
      assert example.method == 'foo'
  </code></pre>
  
  <p>The result is:</p>
  
  <pre><code>&lt;class 'scrapy.selector.lxmlsel.HtmlXPathSelector'&gt;
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 6, in &lt;module&gt;
  AssertionError
  &gt;&gt;&gt;
  </code></pre>
  
  <p>This example is nigh on the same as the one in the Mock library tutorial. Any idea why it's not working?</p>