[英]Django REST UnitTest No file was submitted
I successfully implement with small cases.我成功地实施了小案例。 Then I started to work with bigger structure.然后我开始使用更大的结构。 And I got the error.我得到了错误。
Error:错误:
No file was submitted.
import tempfile
from unittest import skip
from django.conf import settings
from django.contrib.auth.models import User
from django.core.files import File
from django.core.files.uploadedfile import SimpleUploadedFile
from model_mommy import mommy
from rest_framework import status
from rest_framework.reverse import reverse
from rest_framework.test import APITestCase, APIClient
class CustomerFromExcelViewsetTest(APITestCase):
def setUp(self):
self.client = APIClient()
self.soken_staff = mommy.make(User, username='spearhead')
self.user = mommy.make(User, username='Justin')
settings.MEDIA_ROOT = tempfile.mkdtemp()
def test_upload_file(self):
"""Expect created_user, and updated_user correct set"""
file = File(open('./soken_web/apps/uploaded_files/complete-customer.xlsx', 'rb'))
uploaded_file = SimpleUploadedFile('new_excel.xlsx', file.read(), content_type='multipart/form-data')
data = {
file: uploaded_file,
}
self.client.force_authenticate(user=self.user)
response = self.client.post(reverse('api:customer_from_excel-list'), data, format='multipart')
response.render()
self.assertEqual(status.HTTP_201_CREATED, response.status_code)
Here they are the models
, serializers
, and viewsets
它们是models
、 serializers
和viewsets
models.py模型.py
https://gist.github.com/elcolie/52daf2bd144af82b348f7353656be434 https://gist.github.com/elcolie/52daf2bd144af82b348f7353656be434
serializers.py序列化程序.py
https://gist.github.com/elcolie/7f097642c4a752e76044c6938c49e097 https://gist.github.com/elcolie/7f097642c4a752e76044c6938c49e097
viewsets.py视图集.py
https://gist.github.com/elcolie/34fa66632209f14624899d997919d3fb https://gist.github.com/elcolie/34fa66632209f14624899d997919d3fb
After a day I could not figure out where is the that bug.一天后,我无法弄清楚那个错误在哪里。
References:参考:
DRF APITestCase not use `multipart` with other param DRF APITestCase 不将 `multipart` 与其他参数一起使用
looks like you missed quotes in data
dict.看起来你错过了data
字典中的引号。 It should be:它应该是:
data = {
'file': uploaded_file,
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.