To add onto Kit's answer, specifying a 3rd argument to patch.object()
allows the mocked object/method to be specified. Otherwise, a default MagicMock
object is used.
def fake_bar(self):
print "Do something I want!"
return True
@patch.object(my_app.models.FooClass, 'bar', fake_bar)
def test_enter_promotion(self):
self.client.get(reverse(view))
# Do something I want!
Note that, if you specify the mocking object, then the default MagicMock()
is no longer passed into the patched object -- e.g. no longer:
def test_enter_promotion(self, mock_method):
but instead:
def test_enter_promotion(self):
http://www.voidspace.org.uk/python/mock/patch.html#patch-object
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…