In a class, I have an attribute (self.data) which is a pandas.DataFrame.
I have a method save() in the class that basically calls self.data.to_csv() with some validations beforehand. In the test, I would like to patch this so that it won't actually store data in the directory, I just need to make sure it runs as mock.
I couldn't wrap my head around how to patch it. So far I have:
# Myclass.py
import pandas as pd
class Myclass:
def __init__(self, data):
self.data = pd.DataFrame(data=data)
def save(self, path):
# Do something validation
# I would like to patch the line below.
self.data.to_csv(path)
In test_myclass.py:
from unittest import mock
import Myclass
@mock.patch(Myclass.to_csv)
def test_save(myclass_fixture):
myclass_fixture.save(path)
I got the error:
AttributeError: type object 'Portfolio' has no attribute 'to_csv'