【问题标题】:Test written for django URL Dispatcher not working为 django URL Dispatcher 编写的测试不起作用
【发布时间】:2012-06-08 05:09:16
【问题描述】:

我正在 django 中开发一个简单的 web 服务。这是我在 django/python 中的第一个网络应用程序,所以如果我在这里遗漏了一些明显的东西,我不会感到惊讶,但是......

我目前正在尝试测试过滤 url 的逻辑。

            # Works as expected
            response = self.client.post("/mysite/goodurl/")
            self.assertEqual(response.status_code, 200)

            # Has an exception rather than a 404
            response = self.client.post("/mysite/badurl/")
            self.assertEqual(response.status_code, 404)

所以,badurl 案例不仅仅是找不到并抛出 404,而是我收到了这个错误:

Traceback (most recent call last):
  File "/home/user/me/mysite/tests.py", line 55, in test_add_tracker
    response = self.client.post("/mysite/badurl/")
  File "/home/path/to/some/bin/dir/freeware/Python/lib/python2.7/site-packages/django/test/client.py", line 449, in post
    response = super(Client, self).post(path, data=data, content_type=content_type, **extra)
  File "/home/path/to/some/bin/dir/freeware/Python/lib/python2.7/site-packages/django/test/client.py", line 262, in post
    return self.request(**r)
  File "/home/path/to/some/bin/dir/freeware/Python/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/home/path/to/some/bin/dir/freeware/Python/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 77, in wrapped_view
    return view_func(*args, **kwargs)
TypeError: EtimeFetcher() got an unexpected keyword argument 'alias'

我尝试在谷歌上搜索 EtimeFetcher 消息,但没有成功。有什么想法吗?

【问题讨论】:

  • 你有什么观点可以捕捉到所有的 Http404 错误吗?然而,似乎 Django 找到了为 badurl 执行的视图。在代码中搜索包含别名命名参数的语句,例如:“alias=yyyy”。或者可能是一些 url 模式,在 urls.py 中包含“别名”作为额外参数?
  • @Tisho - 你是对的。请将您的评论作为答案,以便我投票/接受。基本上,我有一个映射到我还没有写的视图的 url。当请求处理程序试图遍历 url 列表上的所有视图时,它转到了我还没有写的那个……然后砰!
  • 很高兴听到它有帮助:)

标签: python django testing http-status-code-404


【解决方案1】:

Uou 可能有一些观点来捕捉所有 Http404 错误。然而,似乎 Django 找到了执行 /badurl 的视图。在代码中搜索包含别名命名参数的语句,例如:“alias=yyyy”。或者可能是一些 url 模式,在 urls.py 中包含“别名”作为额外参数?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2019-05-04
    • 2012-03-23
    • 1970-01-01
    • 2011-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多