标题简单描述了我的问题.我想用特定的返回值来模拟"_func_inner_1".谢谢你的任何建议:)
被测代码:
from tornado.gen import coroutine, Return
from tornado.testing import gen_test
from tornado.testing import AsyncTestCase
import mock
@coroutine
def _func_inner_1():
raise Return(1)
@coroutine
def _func_under_test_1():
temp = yield _func_inner_1()
raise Return(temp + 1)
Run Code Online (Sandbox Code Playgroud)
但是,这种直观的解决方案无效
class Test123(AsyncTestCase):
@gen_test
@mock.patch(__name__ + '._func_inner_1')
def test_1(self, mock_func_inner_1):
mock_func_inner_1.side_effect = Return(9)
result_1 = yield _func_inner_1()
print 'result_1', result_1
result = yield _func_under_test_1()
self.assertEqual(10, result, result)
Run Code Online (Sandbox Code Playgroud)
如果出现以下错误,似乎_func_inner_1没有修补,因为它具有协同性质
AssertionError: 2
Run Code Online (Sandbox Code Playgroud)
如果我添加coroutine补丁返回模拟功能
@gen_test
@mock.patch(__name__ + '._func_inner_1')
def test_1(self, mock_func_inner_1):
mock_func_inner_1.side_effect = Return(9)
mock_func_inner_1 = coroutine(mock_func_inner_1)
result_1 = yield …Run Code Online (Sandbox Code Playgroud)