Mocking a class method that is used via an instance

后端 未结 3 1967
清酒与你
清酒与你 2021-02-03 10:25

I\'m trying to patch a class method using mock as described in the documentation. The Mock object itself works fine, but its methods don\'t: For example, their attributes like <

3条回答
  •  自闭症患者
    2021-02-03 11:15

    I mock classmethods like this:

    def raiser(*args, **kwargs):
        raise forms.ValidationError('foo')
    with mock.patch.object(mylib.Commands, 'my_class_method', classmethod(raiser)):
        response=self.admin_client.get(url, data=dict(term='+1000'))
    

提交回复
热议问题