Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 模仿一种方法';s的返回值不起作用_Python 3.x_Mocking_Python Mock - Fatal编程技术网

Python 3.x 模仿一种方法';s的返回值不起作用

Python 3.x 模仿一种方法';s的返回值不起作用,python-3.x,mocking,python-mock,Python 3.x,Mocking,Python Mock,在测试create\u response方法时,我似乎无法模拟get\u external\u response方法的返回值 /foo/response 从abc导入ABCMeta,abstractmethod def create_响应(url,类型): 查询=创建查询(url,类型) external\u response=get\u external\u response(query)我注意到您正在函数内部创建一个Mock对象,但实际上并没有使用Mock。 看起来您需要对用于使用模拟的函数

在测试
create\u response
方法时,我似乎无法模拟
get\u external\u response
方法的返回值

/foo/response

从abc导入ABCMeta,abstractmethod
def create_响应(url,类型):
查询=创建查询(url,类型)

external\u response=get\u external\u response(query)我注意到您正在函数内部创建一个Mock对象,但实际上并没有使用Mock。 看起来您需要对用于使用模拟的函数进行修补

/foo/test\u响应

@mock.patch('foo.response.get_external_response')
def test_create_response(mock_get_external_reponse):
    mock_get_external_response.return_value = 'www'         # This is a Mock object, but will be used as a patch over the actual function where it is used

    result = create_response('www.stackoverflow.com', 'A')
    assert result == 'www'
为方便起见,可快速链接到相关文档部分:

模拟和修补简介:

请在此处特别注意: