Después de leer esto: How do I mock an open used in a with statement (using the Mock framework in Python)?Python Mock - Burlándose varios abierta
soy capaz de burlarse de la función abierta en Python usando:
with patch(open_name, create=True) as mock_open:
mock_open.return_value = MagicMock(spec=file)
m_file = mock_open.return_value.__enter__.return_value
m_file.read.return_value = 'text1'
diffman = Diffman()
diffman.diff(path1, path2)
funciona bien cuando mi método probado utilizaba un estado de cuenta abierta. Aquí está mi método probado:
def diff(self, a, b):
with open(a, 'r') as old:
with open(b, 'r') as new:
oldtext = old.read()
newtext = new.read()
Los valores de oldtext y newtext son los mismos ('text1' aquí).
Me gustaría tener 'text1' para el texto antiguo y 'text2' para el nuevo texto.
¿Cómo puedo hacer esto?