I'm trying to mock something while testing a Django app using the imaginatively named Mock testing library. I can't seem to quite get it to work, I'm trying to do this:
models.py
from somelib import FooClass
class Promotion(models.Model):
foo = models.ForeignKey(FooClass)
def bar(self):
print "Do something I don't want!"
test.py
class ViewsDoSomething(TestCase):
view = 'my_app.views.do_something'
def test_enter_promotion(self):
@patch.object(my_app.models.FooClass, 'bar')
def fake_bar(self, mock_my_method):
print "Do something I want!"
return True
self.client.get(reverse(view))
What am I doing wrong?
bar
is in fact an "instance method," and its only parameter isself
. To be a class method, it would need to be parameterized withcls
, and would be callable asPromotion.foo()
. – cavaunpeu Sep 10 '15 at 19:57@patch.object('my_app.models.FooClass', 'bar')
– Lasma Aug 29 '16 at 22:26cls
, but more importantly (sinceself
andcls
don't mean anything special in python), decorated with@classmethod
– dwanderson Jan 23 '18 at 21:28