python - Django 测试错误

标签 python django testing

我正在尝试为我的 Django 应用程序编写测试,但我遇到了一个我无法解释的错误。由于某种原因,这段代码出现错误

self.client.login(email='example@comcast.net', password='password')
resp = self.client.get(reverse('task:accept_bid', 
    kwargs={'task_pk' : 8, 'bid_pk' : 6}
    ))

但是当我这样做时我得到了一个错误(同样的代码我只是没有登录)

resp = self.client.get(reverse('task:accept_bid', 
    kwargs={'task_pk' : 8, 'bid_pk' : 6}
    ))

这是我得到的回溯

Traceback (most recent call last):
   File "/Users/AdamC/projects/WorkStudy/workstudy/tasks/tests.py", line 37, in test_this
kwargs={'task_pk' : 8, 'bid_pk' : 6}
  File "/Users/AdamC/projects/Environment/WorkStudy-env/lib/python2.7/site-packages/django/test/client.py", line 473, in get
    response = super(Client, self).get(path, data=data, **extra)
   File "/Users/AdamC/projects/Environment/WorkStudy-env/lib/python2.7/site-packages/django/test/client.py", line 280, in get
    return self.request(**r)
  File "/Users/AdamC/projects/Environment/WorkStudy-env/lib/python2.7/site-packages/django/test/client.py", line 444, in request
    six.reraise(*exc_info)
  File "/Users/AdamC/projects/Environment/WorkStudy-env/lib/python2.7/site-packages/django/core/handlers/base.py", line 112, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/AdamC/projects/Environment/WorkStudy-env/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 22, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/Users/AdamC/projects/WorkStudy/workstudy/tasks/views.py", line 44, in accept_a_bid_view
    """)
TypeError: error() takes at least 2 arguments (1 given)

这是 accept_bid_view 代码

@login_required
def accept_a_bid_view(request, **kwargs):
    task=Task.objects.get(pk=kwargs['task_pk'])

    if task.creator == request.user and task.accepted_bid == None:
        bid = Bid.objects.get(pk=kwargs['bid_pk'])
        task.accepted_bid = bid
        task.worker = bid.bidder
        task.accepted = True
        task.accepted_date = datetime.datetime.utcnow().replace(tzinfo=timezone.utc)
        task.save()
        messages.success(request,"Congrats you've accepted a bid I'm proud of you")
    if bid.bidder.email_notifactions:
        send_mail(' Notification',
            "Someone accepted your bid",
            EMAIL_HOST_USER,
            [task.creator.email],
            fail_silently=False)
    return HttpResponseRedirect(reverse('task:task_list'))
    else:
        messages.error("""It looks as if this task has already been accepted
         or maybe you're not the creator, but why would you being clicking 
         buttons then... (The NSA is watchig you)
         """)
    return HttpResponseRedirect(reverse('home'))

最佳答案

您忘记将请求添加到

messages.error("""It looks as if this task has already been accepted
         or maybe you're not the creator, but why would you being clicking 
         buttons then... (The NSA is watchig you)
         """)

它应该看起来像

messages.error(request, """It looks as if this task has already been accepted
         or maybe you're not the creator, but why would you being clicking 
         buttons then... (The NSA is watchig you)
         """)

关于python - Django 测试错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24852846/

相关文章:

python - Django导入导出: Documentation cannot be understood

testing - 开发/QA/生产环境

只需一行 Python 赋值

python - 约束二阶导数时,Scipy CubicSpline 外推如何工作?

python - 从 sqlite 迁移到 postgres (django) 时如何更新单元测试

django - 在 proxypass 后面托管一个 django 项目

python - Django TypeError __str__ 返回非字符串(类型元组)

ruby-on-rails - 为什么 rspec 不使用密码重置 token 更新数据库记录?

testing - 测试人员在敏捷中的角色?

python - 如何计算字符串开头的字符数?