I am writing unittest for a class (MyClass) that has an instance of an object from another class (MyClient). I am not interested on testing MyClient so I want to replace it with an instance I can control. Basically MyClient has a 'connected' property that is being invoked by MyClass. Here is some code
my_class.py
from my_client import MyClient
class MyClass:
def __init__(self):
self.client = MyClient()
def connect(self):
print("Connecting")
self.client.client_connect()
def send(self):
if self.client.connected:
print("Sending message")
def is_connected(self):
return self.client.connected
my_client.py
class MyClient:
def __init__(self):
self.connected = False
def client_connect(self):
print("Client is connecting")
self.connected = True
I want to test the send method, but I can't figure out how to inject a client object whose 'connected' property is set to True. Similar fashion as @MockBean in Java.
Basically something of the sort:
with mock.patch("in the MyClass", "replace MyClient objects/class", "with MyMockedClass/object") as mocked:
# then test the send method
Thanks for your help