title: Mock for line in open(file):
body: <p>I am wanting to unittest a component of my application. The code looks a little like below.</p>
  
  <pre><code>def read_content_generator(myfile):
      for line in open(myfile):
          # do some string manipulation.
          yield result
  </code></pre>
  
  <p>The problem I am having is that I cannot mock the <code>open()</code> functionality within a <code>for</code> loop.</p>
  
  <p>What I am aiming for is a <code>unittest</code> like this: (I know this code is not right but its just an example of what I am trying to do):</p>
  
  <pre><code>def test_openiteration(self):
      with mock.patch('open') as my_openmock:
          my_openmock.return_value = ['1','2','3']
          response = myfunction()
          self.assertEquals([1,2,3], response)
  </code></pre>