lundi 21 décembre 2015

Mocking a class method and changing some object attributes in Python

I am new to mock in Python. I want to know how to replace (mock) a class method while testing with another one, knowing that the original just changes some attributes of self without returning any value. For example:

def some_method(self):   
    self.x = 4   
    self.y = 6   

So here I can't just change the return_value of the mock. I tried to define a new function (that should replace the original) and give it as side_effect to the mock. But how can I make the mocking function change attributes of the object in the class. Here is my code:

@patch('path.myClass.some_method')
def test_this(self,someMethod):

    def replacer(self):
        self.x = 5
        self.y = 16

some_method.side_effect = replacer

So how does Python now understands the self argument of replacer? Is that the self of the test class, or the self as the object of the class under test?

Aucun commentaire:

Enregistrer un commentaire