How to mock a function imported from inside another function or method?
I don't make a habit of this, but sometimes, in an effort to work around a circular import, I'll import a function from inside another function or method like this:
class MyClass:
def my_method(self):
from somewhere import the_thing
x = the_thing()
return x + 4
This works just fine, but I can't figure out how to test it. My usual fails with complaints that module "xyz" has no attribute "the_thing":
from unittest import TestCase
from unittest.mock import patch
class MyTestCase(TestCase):
@patch("path.to.my.module.the_thing")
def test_stuff(self):
...
Is there a way to mock functions imported at runtime? Is there a Better Way to do this?