'Mocking a model class using Pytest
I have a class which I would like to mock. It is a simple model class. I would like this class object to be sent to another method. I would like to have different values for this class's attributes to test (want to create multiple objects and pass to get_api_response method) I want to know how can I mock the class object as json.
model.py
class Customer:
id: int
name: int
address: str
isRepeatedCustomer: bool
def __init__(self) :
self.id: None
self.name: None
self.address: None
self.isRepeatedCustomer: false
return
Service.py
def get_api_response(self, customerObj):
if(customerObj.id is Not None):
//API processing
return api_response
I have used as follows:
with mock.patch('model.Customer') as MockCustomer:
validResponse = json.dumps({"id":324,"name":"abc","address":"Grand Avenue", "isRepeatingCustomer":true})
MockCustomer.return_value = validResponse
I am not sure if this is the right way to do this mocking. How can I pass this mocked object to test get_api_reponse method. How can I make validReponse as an argument so that I can send different json objects to test.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
