![](/img/trans.png)
[英]Why does python mock.patch work differently when second patch argument is used vs return_value?
[英]Python Mock: Unexpected result with patch and return_value
我將首先發布一些代碼,這樣更清楚。
我的課:
from tools import get_knife, sharpen
class Banana(object):
def chop(self):
knife = get_knife()
sharpen(knife)
我的測試:
from mock import patch, sentinel
from banana import Banana
class TestBanana(unittest.TestCase):
@patch('banana.get_knife')
@patch('banana.sharpen')
def test_chop(self, get_knife_mock, sharpen_mock):
get_knife_mock.return_value = sentinel.knife
Banana().chop()
sharpen_mock.assert_called_with(sentinel.knife)
該測試將失敗,因為未使用get_knife_mock的return_value調用sharpen_mock。
請注意,裝飾器是從底部向上應用的。 這是Python應用裝飾器的標准方法。 傳遞到您的測試函數中的已創建模擬的順序與此順序匹配。
http://www.voidspace.org.uk/python/mock/patch.html#nesting-patch-decorators
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.