title: Mocking a class's construction
body: <p>I'm just getting started with using Python's <a href="http://www.voidspace.org.uk/python/mock/index.html" rel="noreferrer">mock</a> library to help write more concise and isolated unit tests. My situation is that I've got a class that reads in data from a pretty hairy format, and I want to test a method on this class which presents the data in a clean
  format.</p>
  <pre><code>class holds_data(object):
      def __init__(self, path):
          &quot;&quot;&quot;Pulls complicated data from a file, given by 'path'.
  
          Stores it in a dictionary. 
          &quot;&quot;&quot;
          self.data = {}
          with open(path) as f:
              self.data.update(_parse(f))
  
      def _parse(self, file):
          # Some hairy parsing code here
          pass
  
      def x_coords(self):
          &quot;&quot;&quot;The x coordinates from one part of the data
          &quot;&quot;&quot;
          return [point[0] for point in self.data['points']]
  </code></pre>
  <p>The code above is a simplification of what I have. In reality <code>_parse</code> is a fairly significant method which I have test coverage for at the functional level.</p>
  <p>I'd like to be able, however, to test <code>x_coords</code> at a unit test level. If I were to instantiate this class by giving it a path, it would violate the <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=126923" rel="noreferrer">rules of unit tests</a> because:</p>
  <blockquote>
  <p>A test is not a unit test if:</p>
  <ul>
  <li>It touches the filesystem</li>
  </ul>
  </blockquote>
  <p>So, I'd like to be able to patch the <code>__init__</code> method for <code>holds_data</code> and then just fill in the part of <code>self.data</code> needed by <code>x_coords</code>. Something like:</p>
  <pre><code>from mock import patch
  with patch('__main__.holds_data.__init__') as init_mock:
      init_mock.return_value = None
      instance = holds_data()
      instance.data = {'points':[(1,1),(2,2),(3,4)]}
      assert(instance.x_coords == [1,2,3])
  </code></pre>
  <p>The code above works but it feels like it's going about this test in a rather roundabout way. Is there a more idiomatic way to patch out a constructor or is this the correct way to go about doing it? Also, is there some code smell, either in my class or test that I'm missing?</p>
  <p><strong>Edit:</strong> To be clear, my problem is that during initialization, my class does significant amounts of data processing to organize the data that will be presented by a method like <code>x_coords</code>. I want to know what is the easiest way to patch all of those steps out, without having to provide a complete example of the input. I want to only test the behavior of <code>x_coords</code> in a situation where I control the data it uses.</p>
  <p>My question of whether or not there is code smell here boils down to this issue:</p>
  <p>I'm sure this would be easier if I refactor to have <code>x_coords</code> be a stand alone function that takes <code>holds_data</code> as a parameter. If &quot;easier to tests == better design&quot; holds, this would be the way to go. However, it would require the <code>x_coords</code> function to know more about the internals of <code>holds_data</code> that I would normally be comfortable with. Where should I make the trade off? Cleaner code or cleaner tests?</p>