Anthony
Anthony

Reputation: 979

Patch class imported inside function in Python

I have:

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()

How do I patch meow()?

I tried:

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

But I keep getting an AttributeError.

Upvotes: 1

Views: 64

Answers (1)

blhsing
blhsing

Reputation: 107124

You can always patch the module/class with its original package/module path:

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

Upvotes: 1

Related Questions