Reputation: 9355
I've written some code that makes calls to the SalesforceIQ API. I'm now trying to unit test my code (writing a unit test for each of my functions). I'm a little unsure how to go about this since I need to do something to avoid making API calls.
Here's an example function I'm trying to test:
def update_contact(name, phone, address, contact_id):
contact = Contact(contact_id)
set_contact_fields(contact, name, phone, address)
return contact.update()
contact.update()
is an API call. So I'd like to avoid calling contact.update()
while still testing update_contact
is updating those three fields (name, phone, address).
Any recs for how to go about this?
Thanks!
Upvotes: 0
Views: 545
Reputation: 4422
Is there a __repr__()
or __str__()
method for the Contact
object? If not, why not write one? Then you can just print the contact
and see what it is. Or do something else with the string it returns if you want to further automate your testing.
Upvotes: 0
Reputation: 12100
You can use patch
(mock.patch
in python2, unittest.mock.patch
in python3) to patch that method:
@patch.object(Contact, 'update')
def test_api_call(self, update):
# "update" is the patched method, instace of `MagicMock`
update_contact('name', 'phone', 'address', 'contact_id')
update.assert_called_once_with()
Upvotes: 1