简体   繁体   English

在 django rest 测试中发出发布请求的问题

[英]Problem with making a post request in django rest testing

I am working on testing a view that requires authentication to make a post request我正在测试一个需要身份验证才能发出发布请求的视图

user = User.objects.get(id=1)
self.client.force_login(user=user)
response = self.client.post(url, data, format="application/json")
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

I am trying to get a specific user object to make force authentication but it fails to retrieve the objects and pops up the following error我正在尝试让特定用户 object 进行强制身份验证,但它无法检索对象并弹出以下错误

System check identified 6 issues (0 silenced).
WARNING:rollbar:Rollbar already initialized. Ignoring re-init.
WARNING:django.request:Unauthorized: /v2/smsreader
.EWARNING:rollbar:Rollbar already initialized. Ignoring re-init.
.
======================================================================
ERROR: test_create_valid (dialer_app.tests.SMSReaderViewTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/ehab/Desktop/raseedi/raseedi/dialer_app/tests.py", line 45, in test_create_valid
    user = RaseediUser.objects.get(id=1)
  File "/home/ehab/Desktop/raseedi/envraseedi/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/ehab/Desktop/raseedi/envraseedi/lib/python3.6/site-packages/django/db/models/query.py", line 408, in get
    self.model._meta.object_name
users.models.User.DoesNotExist: User matching query does not exist.

----------------------------------------------------------------------
Ran 3 tests in 0.031s

FAILED (errors=1)
Destroying test database for alias 'default'...

any help with that error?对该错误有任何帮助吗?

This is a User.DoesNotExist exception.这是一个User.DoesNotExist异常。 This means you don't have any User with provided criteria because your test starts with empty (blank) test database.这意味着您没有任何具有提供标准的User ,因为您的测试从空(空白)测试数据库开始。 If you're using built-in testing framework and want to test database-baked case, you have several options:如果您正在使用内置测试框架并想要测试数据库烘焙案例,您有多种选择:

  1. Make a fixture from development database and load it in the begging of your tests: official docs (double check that you have such User record when dumping data from dev database) Example from offsite:从开发数据库制作一个夹具并在您的测试请求中加载它: 官方文档(从开发数据库转储数据时仔细检查您是否有这样的User记录)来自异地的示例:

from django.test import TestCase from myapp.models import Animal从 django.test 导入 TestCase 从 myapp.models 导入 Animal

class AnimalTestCase(TestCase): fixtures = ['mammals.json', 'birds'] class AnimalTestCase(TestCase):fixtures = ['mammals.json', 'birds']

def setUp(self):
    # Test definitions as before.
    call_setup_methods()

def test_fluffy_animals(self):
    # A test that uses the fixtures.
    call_some_test_code()
  1. Create instances in the beginning of your test as usual.像往常一样在测试开始时创建实例。

Also there is an additional option - data migration, but second option is most easy to use - create needed instance(s) and run your test case.还有一个额外的选项 - 数据迁移,但第二个选项最容易使用 - 创建所需的实例并运行您的测试用例。

Providing initial data for models为模型提供初始数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM