The somewhat unintuitive bit here is that by passing side_effect a function mock will use the result of the function as the return_value for the mocked tempfile.NamedTemporaryFile(). Or as the docs say: "The function is called with the same arguments as the mock, and unless it returns DEFAULT, the return value of this function is used as the return value."
def _GetTempFile(self, *args, **kwargs): tempfile = StringIO.StringIO() tempfile.flush = mock.MagicMock() self.output_streams.append(tempfile) return tempfile with mock.patch.object(tempfile, "NamedTemporaryFile", side_effect=self._GetTempFile): # mocked code...
No comments:
Post a Comment