Mocking properties
Chainmock provides robust support for mocking properties in Python classes. You can mock properties using the standard mock() method - Chainmock will automatically detect that the attribute is a property and create an appropriate property mock.
Basic property mocking
Mock a property to return a specific value:
mocker(Teapot).mock("state").return_value("mocked state")
teapot = Teapot()
assert teapot.state == "mocked state"
Sometimes you might want to force an attribute to be mocked as a property, especially when working with stubs or when automatic detection doesn't work. Use the force_property=True parameter:
stub = mocker()
stub.mock("temperature", force_property=True).return_value(98)
assert stub.temperature == 98
Asserting property access
Assert read access
You can verify that a property was accessed (read):
mocker(Teapot).mock("state").called_once()
teapot = Teapot()
_ = teapot.state # Access the property
Assert that a property was read exactly twice:
mocker(Teapot).mock("state").called_twice()
teapot = Teapot()
_ = teapot.state # First access
_ = teapot.state # Second access
Assert that a property was never accessed:
mocker(Teapot).mock("state").not_called()
Note
For more information, please see also API reference. It contains more examples and extensive documentation about every method and function available in Chainmock.