1
我有两个Python文件:模拟功能
function.py:
def foo():
return 20
def func():
temp = foo()
return temp
和mocking.py:
from testing.function import *
import unittest
import mock
class Testing(unittest.TestCase):
def test_myTest(self):
with mock.patch('function.func') as FuncMock:
FuncMock.return_value = 'string'
self.assertEqual('string', func())
我想嘲笑FUNC,但没有积极的结果。我有AssertionError:'string'!= 20.我该如何正确地模拟它?如果我做mock.patch('func'),我有TypeError:需要一个有效的补丁目标。你提供:'func'。如果我将func移动到mocking.py并调用foo:function.foo(),它可以正常工作。但是,如果我不想将函数从function.py移动到mocking.py,该怎么做?
非常感谢你:-) – 2014-12-04 20:27:43