I have a unittest.mock.PropertyMock
(docs) object created for unit testing purposes, on an attribute within an object.
I no longer have the reference to the PropertyMock
made, as the PropertyMock
was monkeypatched in during test setup.
How can I get access to the PropertyMock
for assertions, given I have the object with the given property
?
from typing import Any
from unittest.mock import PropertyMock
class Foo:
@property
def property_1(self) -> int:
return 1
def make_obj() -> Any:
"""Make some object, not returning a reference to the PropertyMock made inside."""
my_obj = Foo()
type(my_obj).property_1 = PropertyMock(return_value=100)
# NOTE: function doesn't return the PropertyMock, only the object
return my_obj
def test_make_obj() -> None:
made_obj = make_obj()
# We can see the PropertyMock is in place and works
assert made_obj.property_1 == 100
# How can I assert the property was set with 9001? NOTE: I don't have access
# to the PropertyMock made above
made_obj.property_1 = 9001
type(made_obj).property_1.assert_called_once_with(9001) # This fails
# AttributeError: 'int' object has no attribute 'assert_called_once_with'
In other words:
- After monkeypatching in the
PropertyMock
- Calling
type(my_obj).property_1
returns100
- I want it to return the
PropertyMock
used (for assertions)