[英]Python Mock: Unexpected result with patch and return_value
I'll post some code first so it's more clear. 我将首先发布一些代码,这样更清楚。
My class: 我的课:
from tools import get_knife, sharpen
class Banana(object):
def chop(self):
knife = get_knife()
sharpen(knife)
My test: 我的测试:
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)
This test will fail because sharpen_mock wasn't called with the return_value of get_knife_mock. 该测试将失败,因为未使用get_knife_mock的return_value调用sharpen_mock。
Note that the decorators are applied from the bottom upwards.
请注意,装饰器是从底部向上应用的。 This is the standard way that Python applies decorators.
这是Python应用装饰器的标准方法。 The order of the created mocks passed into your test function matches this order.
传递到您的测试函数中的已创建模拟的顺序与此顺序匹配。
http://www.voidspace.org.uk/python/mock/patch.html#nesting-patch-decorators http://www.voidspace.org.uk/python/mock/patch.html#nesting-patch-decorators
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.