Home > Net >  What is the equivalent of side effect for a property mock in unit test?
What is the equivalent of side effect for a property mock in unit test?

Time:12-23

I want to mock a property to return different values, in order, like when you use side_effect on a Magic Mock. Right now I'm doing this:

mock = <my magic mock here>
type(mock).information = PropertyMock()
type(mock).information.side_effect = [1,2]

mock.information # it does not return 1
mock.information # it does not return 2

Any idea how to mock it properly?

CodePudding user response:

from unittest.mock import MagicMock, PropertyMock

m = MagicMock()
type(m).foo = PropertyMock(side_effect=[1, 2])

print(m.foo) # 1
print(m.foo) # 2
  • Related