According to the documentation .return_value should be identical to the object returned when calling the mock ("assert m() is m.return_value")
This is the case except on objects returned by __iter__ on MagicMocks. The following script demonstrates the problem:
----
from unittest.mock import MagicMock
m = MagicMock()
assert x.__iter__() is x.__iter__.return_value # <- fails
----
In fact __iter__() returns the object "iter([])" (which matches the documentation) while __iter__.return_value return a MagicMock object (which does not match the documentation).
When replacing "__iter__" with any other special function MagicMock works as expected. |