I'm trying to test some code that operates on a file, and I can't seem to get my head around how to replace using a real file with mock
and io.StringIO
My code is pretty much the following:
class CheckConfig(object):
def __init__(self, config):
self.config = self._check_input_data(config)
def _check_input_data(self, data):
if isinstance(data, list):
return self._parse(data)
elif os.path.isfile(data):
with open(data) as f:
return self._parse(f.readlines())
def _parse(self, data):
return data
I have a class that can take either a list or a file, if it's a file it opens it and extracts the contents into a list, and then does what it needs to do to the resulting list.
I have a working test as follows:
def test_CheckConfig_with_file():
config = 'config.txt'
expected = parsed_file_data
actual = CheckConfig(config).config
assert expected == actual
I want to replace the call to the filesystem. I have tried replacing the file with io.StringIO
but I get a TypeError
from os.path.isfile()
as it's expecting either a string, bytes or int. I also tried mocking the isfile
method like so:
@mock.patch('mymodule.os.path')
def test_CheckConfig_with_file(mock_path):
mock_path.isfile.return_value = True
config = io.StringIO('data')
expected = parsed_file_data
actual = CheckConfig(config).config
assert expected == actual
but I still get the same TypeError
as the _io.StringIO
type is causing the exception before isfile
gets a chance to return something.
How can I get os.path.isfile
to return True, when I pass it a fake file? Or is this a suggestion I should change my code?