title: Customizing unittest.mock.mock_open for iteration
body: <p>How should I customize unittest.mock.mock_open to handle this code?</p>
  
  <pre><code>file: impexpdemo.py
  def import_register(register_fn):
      with open(register_fn) as f:
          return [line for line in f]
  </code></pre>
  
  <p>My first attempt tried <code>read_data</code>.</p>
  
  <pre><code>class TestByteOrderMark1(unittest.TestCase):
      REGISTER_FN = 'test_dummy_path'
      TEST_TEXT = ['test text 1\n', 'test text 2\n']
  
      def test_byte_order_mark_absent(self):
          m = unittest.mock.mock_open(read_data=self.TEST_TEXT)
          with unittest.mock.patch('builtins.open', m):
              result = impexpdemo.import_register(self.REGISTER_FN)
              self.assertEqual(result, self.TEST_TEXT)
  </code></pre>
  
  <p>This failed, presumably because the code doesn't use read, readline, or readlines. 
  The <a href="http://docs.python.org/3.4/library/unittest.mock.html#mock-open">documentation</a> for unittest.mock.mock_open says, "read_data is a string for the read(), readline(), and readlines() methods of the file handle to return. Calls to those methods will take data from read_data until it is depleted. The mock of these methods is pretty simplistic. If you need more control over the data that you are feeding to the tested code you will need to customize this mock for yourself. read_data is an empty string by default."</p>
  
  <p>As the documentation gave no hint on what kind of customization would be required I tried <code>return_value</code> and <code>side_effect</code>. Neither worked.</p>
  
  <pre><code>class TestByteOrderMark2(unittest.TestCase):
      REGISTER_FN = 'test_dummy_path'
      TEST_TEXT = ['test text 1\n', 'test text 2\n']
  
      def test_byte_order_mark_absent(self):
          m = unittest.mock.mock_open()
          m().side_effect = self.TEST_TEXT
          with unittest.mock.patch('builtins.open', m):
              result = impexpdemo.import_register(self.REGISTER_FN)
              self.assertEqual(result, self.TEST_TEXT)
  </code></pre>