本文介紹了使用 mock patch 模擬實例方法的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我在測試 Django 應(yīng)用程序時嘗試模擬一些東西,使用富有想象力的名稱 模擬測試庫.我似乎無法讓它工作,我正在嘗試這樣做:
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))
我做錯了什么?
推薦答案
啊,我對在哪里應(yīng)用補丁裝飾器感到困惑.固定:
Ah I was confused on where to apply that patch decorator. Fixed:
class ViewsDoSomething(TestCase):
view = 'my_app.views.do_something'
@patch.object(my_app.models.FooClass, 'bar')
def test_enter_promotion(self, mock_method):
self.client.get(reverse(view))
這篇關(guān)于使用 mock patch 模擬實例方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!