简体   繁体   English

django测试RequestFactory无法获取路由参数

[英]django test RequestFactory cannot get route parameter to work

I have a problem that I don't know how to make it work. 我有一个问题,我不知道如何使它工作。

urls.py: urls.py:

urlpatterns = [
    url(r'athletes/search$', SearchAthletes.as_view()),
    url(r'athletes/([0-9]+)$', ViewAthlete.as_view())
]

views.py: views.py:

class ViewAthlete(APIView):

    def get(self, request, id, format=None):
        athlete = Athlete.objects.get(id=id)
        serializer = AthleteSerializer(athlete)
        return Response(serializer.data)

test.py: test.py:

def test_view_athlete(self):
    tmp = Athlete.objects.order_by('?')[0]

    request = self.factory.get('/_api/v1/athletes/' + str(tmp.id))
    request.user = AnonymousUser()

    response = ViewAthlete.as_view()(request)
    self.assertEquals(response.data.id, tmp.id)

I keep getting the following error: 我一直收到以下错误:

Traceback (most recent call last): File "/tests.py", line 44, in test_view_athlete response = ViewAthlete.as_view()(request) 回溯(最近一次调用最后一次):文件“/tests.py”,第44行,在test_view_athlete response = ViewAthlete.as_view()(请求)

File "/venv/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view return view_func(*args, **kwargs) 文件“/venv/lib/python3.5/site-packages/django/views/decorators/csrf.py”,第58行,在wrapped_view中返回view_func(* args,** kwargs)

File "/venv/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view return self.dispatch(request, *args, **kwargs) 文件“/venv/lib/python3.5/site-packages/django/views/generic/base.py”,第68行,在视图中返回self.dispatch(request,* args,** kwargs)

File "/venv/lib/python3.5/site-packages/rest_framework/views.py", line 474, in dispatch response = self.handle_exception(exc) 文件“/venv/lib/python3.5/site-packages/rest_framework/views.py”,第474行,在调度响应中= self.handle_exception(exc)

File "/venv/lib/python3.5/site-packages/rest_framework/views.py", line 471, in dispatch response = handler(request, *args, **kwargs) TypeError: get() missing 1 required positional argument: 'id' 文件“/venv/lib/python3.5/site-packages/rest_framework/views.py”,第471行,在调度响应中= handler(request,* args,** kwargs)TypeError:get()缺少1个必需的位置参数: 'ID'

To my understanding, the problem is that, there is no id parameter passed to get function of ViewAthelete view class. 据我了解,问题是,没有传递id参数来get ViewAthelete视图类的功能。 What is the reason for this? 这是什么原因? In development environment (not testing), it displays the data but testing environment doesn't recognize the arguments from the route. 在开发环境(不测试)中,它显示数据,但测试环境无法识别路径中的参数。

As zsepi says, your URLs aren't used here. 正如zsepi所说,这里不使用您的网址。 To avoid repeating the arguments, rather than calling the view directly you could use the test client to "call" the URL: another advantage of doing this is that the middleware runs, so you don't need to assign the user attribute separately. 为避免重复参数,而不是直接调用视图,您可以使用测试客户端“调用”URL:执行此操作的另一个好处是中间件运行,因此您无需单独分配用户属性。

response = self.client.get('/_api/v1/athletes/' + str(tmp.id)) 

AFAIK urlpatterns are considered when testing through the full django request stack, eg: through django.test.Client , using it's get / post methods 在通过完整的django请求堆栈进行测试时会考虑使用AFAIK urlpatterns ,例如:通过django.test.Client ,使用它的get / post方法

When testing your view directly ( MyView.as_view()(request) ) the whole url resolver logic is bypassed, and then the args / kwargs need to be supplied by the caller (eg: MyView.as_view()(request, 'arg1', 'arg2', id='34') ) 直接测试视图时( MyView.as_view()(request) )绕过整个url解析器逻辑,然后调用者需要提供args / kwargs (例如: MyView.as_view()(request, 'arg1', 'arg2', id='34')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM