mercredi 10 janvier 2018

Creating mock object that returns another mock object with function with defined return_value in Python

What I want to do:

from path.to.exec.module import my_function

@patch('path.to.exec.module.TargetClass')
def test(mock_TargetClass):
    mock_TargetClass.return_value = Mock(is_valid.return_value = False) # It doesn't work, SyntaxError
    my_function()

In path.to.exec.module:

form = TargetClass('some args that I dont want to care about')
form.is_valid() # Should return True/False, depends what I set in test function  

It is obvious I can't modify code in path.to.exec.module to set form.is_valid.return_value, but I don't know how to create Mock object that has return_value for specific named method.

I feel it is not very complicated, but I tried different ways, without succeed.

Aucun commentaire:

Enregistrer un commentaire