I have a logger class that needs to write strings to file. So, I have a method like so:
def write_to_file(self, string):
self.__file_handle.write(string)
Note that error handling has been edited out. Clearly I want to test this without writing to a file. Thus mocks via Mock. I have seen this which explains how to mock open but it does not help me here -- I open the file_handle in __init__
. Now, I can do that in setUp() but the mock_open seems to go out of scope after setUp and thus is of no use in the test case.
How would you write a test method to test the write_to_file method using Mock?