Django测试-TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是'User'

时间:2018-08-06 05:16:14

标签: django django-testing django-mixer

我正在为第一个this.activatedRoute.snapshot.data['data']应用程序编写测试用例,并使用Django为某些模块生成随机值。

为在模型上测试而编写的测试用例就像

test_model.py

mixer

但是在运行测试时

from datetime import datetime, timedelta

from django.core.exceptions import ValidationError
from tzlocal import get_localzone

import pytest
from django.test import TestCase
from mixer.backend.django import mixer

from transactions.models import ModeOfPayment, AmountGiven

pytestmark = pytest.mark.django_db

@pytest.mark.django_db
class TestAmountReturned(TestCase):
    def test_model_amount_return_add(self):
        amount_returned = mixer.blend(
            'transactions.AmountReturned',
            amount_given=mixer.blend(
                'transactions.AmountGiven',
                given_date=datetime.now(get_localzone()) - timedelta(days=300)
            ),
            amount=100.00,
            return_date=datetime.now(get_localzone()) - timedelta(days=50)
        )

        assert str(amount_returned) == str(amount_returned.amount), '__str__ should return amount string'

    def test_model_amount_due(self):
        amount = 10000.00
        interest_rate = 9.5
        duration = 365
        given_date = datetime.now(get_localzone()) - timedelta(days=200)
        returned_amount = 150.00

        amount_given = mixer.blend(
            'transactions.AmountGiven',
            contact=mixer.blend('contacts.Contact'),
            amount=amount,
            interest_rate=interest_rate,
            duration=duration,
            given_date=given_date,
            mode_of_payment=mixer.blend('transactions.ModeOfPayment', title='Cash')
        )

        mixer.blend(
            'transactions.AmountReturned',
            amount_given=amount_given,
            amount=returned_amount,
            return_date=datetime.now(get_localzone()) - timedelta(days=50)
        )

        assert amount_given.amount_due == amount_given.total_payable - returned_amount, 'Should return dues amount'

出现以下错误

pipenv run py.test

我的测试案例中错误的位置指向

______________________________ TestAmountReturned.test_model_amount_due _______________________

self = <django.db.models.fields.AutoField: id>, value = <User: khess>

    def to_python(self, value):
        if value is None:
            return value
        try:
>           return int(value)
E           TypeError: int() argument must be a string, a bytes-like object or a number, not 'User'

../../../.local/share/virtualenvs/koober-py-McGChbzt/lib/python3.6/site-packages/django/db/models/fields/__init__.py:940: TypeError

During handling of the above exception, another exception occurred:

self = <transactions.tests.test_models.TestAmountReturned testMethod=test_model_amount_due>

    def test_model_amount_due(self):
        amount = 10000.00
        interest_rate = 9.5
        duration = 365
        given_date = datetime.now(get_localzone()) - timedelta(days=200)
        returned_amount = 150.00

        amount_given = mixer.blend(
            'transactions.AmountGiven',
>           contact=mixer.blend('contacts.Contact'),
            amount=amount,
            interest_rate=interest_rate,
            duration=duration,
            given_date=given_date,
            mode_of_payment=mixer.blend('transactions.ModeOfPayment', title='Cash')
        )

src/transactions/tests/test_models.py:182: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

,但无法确定此错误发生在哪一列。我也使用混音器将联系人与其他位置融合在一起,其中很少能正常工作。

0 个答案:

没有答案