I am wanting to unittest a component of my application. The code looks a little like below.
def read_content_generator(myfile):
for line in open(myfile):
# do some string manipulation.
yield result
The problem I am having is that I cannot mock the open()
functionality within a for
loop.
What I am aiming for is a unittest
like this: (I know this code is not right but its just an example of what I am trying to do):
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)