【问题标题】:DJANGO TESTS: TypeError: test_suit_row_attributes() missing 1 required positional argument: 'request'DJANGO 测试:TypeError:test_suit_row_attributes() 缺少 1 个必需的位置参数:“请求”
【发布时间】:2020-03-24 13:12:06
【问题描述】:

我想解决这个错误并完成测试:

admin.py

def suit_row_attributes(self, obj, request):
    """Add colours to the rows according to the status"""

    type_error = 'notSent'
    status_colours = {Transaction.STATUS_REJECTED: 'error', Transaction.STATUS_RECEIVED: 'received',
                      Transaction.STATUS_PENDING: 'warning', Transaction.STATUS_ACCEPTED: 'success',
                      type_error: 'notSent'}
    try:
        tt_status = Transaction.objects.get(txid=obj.numero).last_status
    except Transaction.DoesNotExist:
        tt_status = type_error
    return {'class': status_colours.get(tt_status, 'success')}

在tests.py中

def test_suit_row_attributes(self):
    self.assertEqual(self.admin_instance.suit_row_attributes(self.errortr_obj), {'class': 'notSent'})

有人可以帮帮我吗?

【问题讨论】:

  • 能否提供实际的错误信息?
  • 这是当前的错误消息:test_suit_row_attributes() 缺少 1 个必需的位置参数:'request'
  • 我已经解决了。 with this: #Add this in the def setup(self): self.request = mock.Mock() def test_suit_row_attributes(self): self.assertEqual(self.admin_instance.suit_row_attributes(self.errortr_obj, self.request), {'类':'notSent'})
  • 非常好!您能否将其添加为下面的答案并提供其工作原理? :)

标签: django testing request django-tests django-suit


【解决方案1】:
from django.contrib.admin import ModelAdmin

class CountryAdmin(ModelAdmin):
    ...

    def suit_row_attributes(self, obj, request):
        return {'class': 'type-%s' % obj.type}

    # Or bit more advanced example
    def suit_row_attributes(self, obj, request):
        css_class = {
            1: 'success',
            0: 'warning',
            -1: 'error',
        }.get(obj.status)
        if css_class:
            return {'class': css_class, 'data': obj.name}

更多信息:- https://django-suit.readthedocs.io/en/develop/list_attributes.html

【讨论】:

    猜你喜欢
    • 2018-01-24
    • 2018-11-18
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 2021-08-12
    • 2020-05-07
    • 2018-12-12
    相关资源
    最近更新 更多