Skip to content

Comment on Super Simple Mocking For Python

Comments

The line where the original attributes are backed up needs to be a deep copy (otherwise the updates will affect the backed up dictionary):

self.orig = self.obj.__dict__

Should be more like:

self.orig = self.obj.__dict__.copy()

Agreed. I did the following:

    def __enter__(self):
        self.original = dict(self.obj.__dict__)
        self.obj.__dict__.update(self.mocks)
        return self

    def __exit__(self, typ, val, traceback):
        self.obj.__dict__ = self.original
This doesn't perform a deep copy, but prevents modifications on self.obj.__dict__ from modifying self.original also.
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.