Created
October 3, 2016 11:03
-
-
Save igniteflow/1be88c18185ffe42a66a46e48118f486 to your computer and use it in GitHub Desktop.
How to mock an object property in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mock | |
with mock.patch('path.to.ObjectClass.my_property', new_callable=mock.PropertyMock) as mock_my_property: | |
mock_my_property.return_value = 'my value' |
Good one. Thanks!
Thank you!
This also works:
import mock
with mock.patch("...", new_callable=mock.PropertyMock, return_value="my value"):
...
Can someone please demonstrate how to do it in other forms? I.e. without a context manager
HI I am using the same thing . The property is getting mocked properly with the proper value that i have set , but when i call the method to test which uses this property , the actual property is getting called instead of this mock that i have created .
Can someone please demonstrate how to do it in other forms? I.e. without a context manager
there is one decorator method:
@patch("...", new_callable=mock.PropertyMock,return_value="some_value")
def test_fun(self, mock):
pass
Cool. Thanks. It saved me hours.
Can someone please demonstrate how to do it in other forms? I.e. without a context manager
there is one decorator method:
@patch("...", new_callable=mock.PropertyMock,return_value="some_value") def test_fun(self, mock): pass
@patch('path.to.ObjectClass.my_property', new_callable=mock.PropertyMock)
def test_fun(self, mock):
mock.return_value="some_value"
pass
Thanks!
@lhoncorty Thank you!
thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks!