title: Mocking file reads using patch decorator and side_effect
body: <p>I am testing a function that reads a file, does operations to the contents and returns a value depending on what it sees. The function <code>testme</code> I test lives in <code>module.py</code>. I am running python 2.7. I know I can accomplish this with</p>
  
  <pre><code>import unittest
  import module
  from mock import patch, mock_open
  
  TestTestMe(unittest.TestCase):
      ...
      def test_test_me(self):
          with patch('module.open', mock_open(read_data='1 2')) as _:
            self.assertRaises(IndexError, module.testme, 'foo')
          with patch('module.open', mock_open(read_data='1 2 3')) as _:
            self.assertEquals(module.testme('foo'), 3)
  </code></pre>
  
  <p>etc.</p>
  
  <p>However, I would like to (mostly to prevent repeated use of with statement, and also to be able to dynamically generate various read_data) be able to do this using the <code>@patch</code> as decorator defining my read_data with a function. Something like this. I will not repeat the class definition and imports.</p>
  
  <pre><code>def give_contents(x):
      if x == 'correct':
          return mock_open(read_data='1 2 3')
      else:
          return mock_open(read_data='a b c')
  </code></pre>
  
  <p>and then using the test function like:</p>
  
  <pre><code>@patch(module.open, side_effect=give_contents)
  def test_test_me(self):
      self.assertEquals(module.testme('correct'), 3)
  </code></pre>
  
  <p>I keep running into TypeErrors such as </p>
  
  <pre><code>TypeError: test_testme() takes exactly 1 argument (2 given)
  </code></pre>
  
  <p>however I try to get around this. This is driving me crazy. Guidance would be greatly appreciated. If you want some additional details I may have omitted, please ask for specifics, and I will provide those.</p>
  
  <p>Edit: Implementation of the function to be tested as requested. I'm sorry I omitted this as 'unimportant', it obviously should have been there.</p>
  
  <pre><code>def testme(filepath):
      with open(filepath, 'r') as f:
          line = f.readline().strip().split()
      return int(line[2])
  </code></pre>