[Tutor] Use case of attaching list of strings as spec to the Mock.mock_add_spec() method?
Arup Rakshit
ar at zeit.io
Sun Apr 28 10:30:11 EDT 2019
Code:
from unittest.mock import Mock
class C:
def get_val(self):
print(1)
mock = Mock()
mock.mock_add_spec(C())
mock.get_val.side_effect = C().get_val
print(isinstance(mock, C))
True
mock.get_val()
1
In the above code, if I replace `mock.get_val.side_effect = C().get_val` with `mock.get_val.return_value = 1` the code still works same.
from unittest.mock import Mock
class C:
def get_val(self):
print(1)
mock = Mock()
mock.mock_add_spec(C())
mock.get_val.return_value = 1
print(isinstance(mock, C))
True
mock.get_val()
1
So question is that: Are there any difference using side_effect and return_value while configuring a return value of a method?
From doc of [mock_add_spec()](https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.mock_add_spec) it says:
> Add a spec to a mock. spec can either be an object or a list of strings.
What kind of string it refers here? any random valid strings? Why would some one try to mock a string objects instead of just using that string object itself? How does this part work while attaching a spec to the Mock?
I understand the object spec part, not just the list of strings use case. So if anyone can explain this, I will be very helpful.
Thanks,
Arup Rakshit
ar at zeit.io
More information about the Tutor
mailing list