简体   繁体   English

Django 更新视图在测试期间不更新 object

[英]Django Update view doesn't update an object during test

I'm writing tests for my views and I'm stuck with the UpdateView and the POST request.我正在为我的视图编写测试,但我坚持使用 UpdateView 和 POST 请求。 For this simple test I try just to change first_name but assertion fails.对于这个简单的测试,我尝试只更改 first_name 但断言失败。 What am I doing wrong?我究竟做错了什么? However, when I call the response.context it gives me that (it has the updated employee):但是,当我调用 response.context 时,它给了我(它有更新的员工):

[{'True': True, 'False': False, 'None': None}, {'csrf_token': <SimpleLazyObject: <function csrf.<locals>._get_val at 0x7f070ed6eee0>>, 'request': <WSGIRequest: POST '/employees/7/update'>, 'user': <SimpleLazyObject: <User: testuser@test.com>>, 'perms': <django.contrib.auth.context_processors.PermWrapper object at 0x7f070ecb33d0>, 'messages': <django.contrib.messages.storage.fallback.FallbackStorage object at 0x7f070ed1b130>, 'DEFAULT_MESSAGE_LEVELS': {'DEBUG': 10, 'INFO': 20, 'SUCCESS': 25, 'WARNING': 30, 'ERROR': 40}}, {}, {'object': <Employee: John Smith>, 'employee': <Employee: John Smith>, 'form': <EmployeeUpdateForm bound=True, valid=False, fields=(first_name;last_name;user;position;reports_to;birthday;hire_date;address;phone_number;pfp)>, 'view': <employees.views.EmployeesUpdateView object at 0x7f070ed1b310>}]

The test:考试:

class TestEmployeesUpdateView(TestCase):

def setUp(self):
    self.test_user = User.objects.create_user(
        username='test_user', email=
        'testuser@test.com', password='Testing12345')
    self.test_employee = Employee.objects.create(
        first_name='Bob', last_name='Smith', user=self.test_user, position='SM',
        birthday=date(year=1995, month=3, day=20),
        hire_date=date(year=2019, month=2, day=15),
        address='...',
    )
    self.client = Client()


def test_updateview_post(self):
    self.client.force_login(user=self.test_user)
    response = self.client.post(reverse('employees:employee-update', kwargs={'pk': self.test_employee.pk}), {'first_name': 'John'})
    self.test_employee.refresh_from_db()
    self.assertEqual(self.test_employee.first_name, 'John')

The view:风景:

class EmployeesUpdateView(LoginRequiredMixin, UpdateView):
model = Employee
template_name = 'employees/employee_details.html'
form_class = EmployeeUpdateForm

And the error:和错误:

FAIL: test_updateview_post (employees.tests.test_views.TestEmployeesUpdateView)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/main/dev/project1/employees/tests/test_views.py", line 63, in test_updateview_post
    self.assertEqual(self.test_employee.first_name, 'John')
AssertionError: 'Bob' != 'John'
- Bob
+ John

Edit: typo.编辑:错字。

You got a typo in your test_updateview_post() It is {'frist_name': 'John'}你的 test_updateview_post() 中有错字是 {'frist_name': 'John'}

Judging from you User, Employee Model I don't see any frist_name从你的用户来看,员工 Model 我没有看到任何 frist_name

Your form is not valid, check response.context['form'].errors for errors.您的表单无效,请检查 response.context['form'].errors 是否有错误。

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

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