How to mock singleton instance methods?

Viewed 21

I have a module written roughyl as follows:

class __Foo:
  def __init__(self):
    self.__client = CeateClient() # a library function

  async def call_database(self):
    self.__client.do_thing()

foo = __Foo()

This is designed so that the entire server only has one instance of this client manage, so foo is created once and is then used by various modules.

I am required to now use pytest to, well, test everything, but reading online I cannot find the correct way to path this situation. The class method call_database sends an API request to a different service, which as part of the test, I want to not actually happen. The suggestions I see talk about patching the class method itself, but also the class is private so I couldn't really do that.

The point is, when testing a function in a different module, i.e.:

from foo import foo

async def bar():
  await foo.call_database()

When testing the function bar, I want the call to the database to not happen. How would I go about doing that?

0 Answers
Related