在Python内部函数中导入的补丁类

时间:2019-11-05 00:48:35

标签: python mocking

我有:

folder / cats.py

class Cat(object):
    def __init__(self, color):
        self.color = color

    def meow():
        pass

folder / something.py

def something():
    from folder.cats import Cat

    scootish_fold = Cat(color='Black')
    scootish_fold.meow()

如何修补meow()

我尝试过:

@patch('folder.something.Cat.meow')
def test_meow(self, cat_meow_patch):
    cat_patch.return_value = 'MEOWW!'

但是我不断收到AttributeError

1 个答案:

答案 0 :(得分:1)

您始终可以使用其原始包/模块路径来修补模块/类:

@patch('folder.cats.Cat.meow')
def test_meow(self, cat_meow_patch):
    cat_patch.return_value = 'MEOWW!'