How to patch return_value attribute?
xmalter opened this issue · 2 comments
xmalter commented
Hello, sorry for opening issue, didn't find a better place to ask.
I'm wondering how I can replace this code:
with patch("service.services.MyService") as mock:
mock.return_value.attribute = {'key': 'value'}
using mocker, I've tried this code, but it didn't work:
mocker.patch(
"service.services.MyService.attribute",
return_value={'key': 'value'},
create=True # otherwise it fails with AttributeError
)
nicoddemus commented
Hi @xmalter,
It is similar, mocker.patch
also return a MagicMock
:
mock = mocker.patch("service.services.MyService.attribute")
mock.return_value.attribute = {'key': 'value'}
xmalter commented
Thanks!
Actually it's
mock = mocker.patch("service.services.MyService") # <- No attribute here
mock.return_value.attribute = {'key': 'value'}
Otherwise it fails with AttributeError