Dopo aver letto questo: How do I mock an open used in a with statement (using the Mock framework in Python)?Python Mock - Mocking diversi aperto
io sono in grado di prendere in giro la funzione di apertura in Python utilizzando:
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)
funziona bene quando il mio metodo collaudato utilizzato una dichiarazione aperta. Qui è il mio metodo collaudato:
def diff(self, a, b):
with open(a, 'r') as old:
with open(b, 'r') as new:
oldtext = old.read()
newtext = new.read()
I valori di oldtext e nuovoTesto sono gli stessi ('text1' qui).
Mi piacerebbe avere 'text1' per il vecchio testo e 'testo2' per il nuovo testo.
Come posso fare questo?