2011-04-10 57 views
2

我會先發布一些代碼,以便更清楚。Python模擬:帶補丁和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) 

這個測試將失敗,因爲sharpen_mock沒有與get_knife_mock的RETURN_VALUE調用。

回答