0

Just started learning/applying Unittest and I have this class method save():

class A():

    __file_path = "file.json"
    __objects = {}

    def save(self):
        """
        serializes __objects to the JSON file: __file_path
        Note:
        __objects is a dictionary of Keys: numbers
        .                            Values: object of class B with a get_dict() method returning dictionary representation a B.object
        """
        for k, v in self.__objects.items():
            Dict[k] = v.get_dict()
        with open(self.__file_path, 'w') as fd:
            json.dump(Dict, fd)

How to approach this method for a unit test?

petezurich
  • 9,280
  • 9
  • 43
  • 57
Ghost_tn
  • 1
  • 1
  • This should help https://stackoverflow.com/questions/33184342/how-do-i-mock-an-open-write-without-getting-a-no-such-file-or-directory – Keshav Biyani Oct 30 '21 at 18:17
  • Refactor: Pass in the open file (i.e. stream-like object) to the save method. Why does your object need to be aware of the file it is stored in? – Ulrich Eckhardt Oct 30 '21 at 18:53

0 Answers0