Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/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
PythonDjango:开始使用mock_Python_Django_Unit Testing_Mocking - Fatal编程技术网

PythonDjango:开始使用mock

PythonDjango:开始使用mock,python,django,unit-testing,mocking,Python,Django,Unit Testing,Mocking,我有以下代码,我正在尝试创建一个测试(仍在进行中): 它失败是因为当我尝试创建InstituiCoForm时,调用了get_请求: def get_request(): return getattr(THREAD_LOCAL, 'request', None) 这就是错误 entidades/tests.py:11: in <module> class InstituicaoFormTestCase(BaseTestCase): entidades/tests.py

我有以下代码,我正在尝试创建一个测试(仍在进行中):

它失败是因为当我尝试创建InstituiCoForm时,调用了get_请求:

def get_request():
    return getattr(THREAD_LOCAL, 'request', None)
这就是错误

entidades/tests.py:11: in <module>
    class InstituicaoFormTestCase(BaseTestCase):
entidades/tests.py:16: in InstituicaoFormTestCase
    @patch('get_request', return_value={'user': 'usuario_qualquer'})
.tox/unit/local/lib/python2.7/site-packages/mock/mock.py:1670: in patch
    getter, attribute = _get_target(target)
.tox/unit/local/lib/python2.7/site-packages/mock/mock.py:1522: in _get_target
    (target,))
E   TypeError: Need a valid target to patch. You supplied: 'get_request'
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> entering PDB >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> /home/vinicius/telessaude/.tox/unit/local/lib/python2.7/site-packages/mock/mock.py(1522)_get_target()
-> (target,))
entidades/tests.py:11:in
类InstituiCoFormTestCase(BaseTestCase):
entidades/tests.py:16:在InstituicaoFormTestCase中
@修补程序('get_request',return_value={'user':'usuario_qualquery'})
.tox/unit/local/lib/python2.7/site-packages/mock/mock.py:1670:在补丁中
getter,attribute=\u get\u target(目标)
.tox/unit/local/lib/python2.7/site packages/mock/mock.py:1522:in\u get\u target
(目标,)
E TypeError:需要一个有效的目标进行修补。您提供了:“获取请求”
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>进入PDB>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>/home/vinicius/telessaude/.tox/unit/local/lib/python2.7/site packages/mock/mock.py(1522)\u get\u target()
->(目标,)

我做错了什么?应该如何模拟这个get_request()方法?

我认为您试图做的具体事情可以这样做:

@patch('core.views.get_request', return_value={'user': 'usuario_qualquer'})
但是如果你还没有,你也应该看看。您可以使用测试客户端来伪造web请求


如果您想尝试不访问数据库的模拟测试,请签出。(我是该项目的一个小贡献者。)我也尝试过模仿视图,但这很微妙。

我认为您尝试做的具体事情可以这样做:

@patch('core.views.get_request', return_value={'user': 'usuario_qualquer'})
但是如果你还没有,你也应该看看。您可以使用测试客户端来伪造web请求


如果您想尝试不访问数据库的模拟测试,请签出。(我是该项目的一个小贡献者。)我也尝试过模拟视图,但这很麻烦。

请参阅上的模拟文档。请注意,使用threadlocals通常被认为是一个坏主意,在Django代码中很少需要这样做。请注意,使用threadlocals通常被认为是一个坏主意,在Django代码中很少需要这样做。救了我的命!谢谢你救了我的命!谢谢