我已经获得了代码和测试文件:
code.py
class Code:
def do_something_inside(self, a, b, c):
return a-b-c
def do_something(self, b, c):
self.do_something_inside(30, b, c)
test.py
import unittest
import unittest.mock as mock
from code import Code
class TestStringMethods(unittest.TestCase):
def setUp(self):
self.code = Code()
def do_something_inside_stub(self, a, b, c):
return a+b+c
@mock.patch('code.Code.do_something_inside', new_callable=do_something_inside_stub)
def test_add(self):
self.assertEquals(self.code.do_something(10, 5), 45)
if __name__ == '__main__':
unittest.main()
我想使用do_something_inside_stub来模拟do_something_inside方法,但执行失败:
E
======================================================================
ERROR: test_add (__main__.TestStringMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python3.6/unittest/mock.py", line 1171, in patched
arg = patching.__enter__()
File "/usr/lib/python3.6/unittest/mock.py", line 1293, in __enter__
new = Klass(**_kwargs)
TypeError: do_something_inside_stub() missing 4 required positional arguments: 'self', 'a', 'b', and 'c'
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)
我不知道如何将参数传递给mock.patch中的new_callable。
答案 0 :(得分:1)
new_callable需要一个继承自Mock的类,其对象将为此mock创建。您正在寻找的功能是side_effect。参考:https://docs.python.org/3/library/unittest.mock.html#the-mock-class
使用以下代码:
@mock.patch('code.Code.do_something_inside', side_effect=do_something_inside_stub)
def test_add(self, do_something_inside_mock):
do_something_inside_mock.assert_called