Đây là phiên bản cũ, nhưng tôi đã gặp sự cố tương tự khi thực hiện kiểm tra đơn vị. Đây là cách tôi giải quyết vấn đề.
Bạn có thể sử dụng response.redirect_chain
và/hoặc response.request['PATH_INFO']
để lấy url chuyển hướng.
Kiểm tra tài liệu! Django Testing Tools: assertRedirects
from django.core.urlresolvers import reverse
from django.test import TestCase
class MyTest(TestCase)
def test_foo(self):
foo_path = reverse('foo')
bar_path = reverse('bar')
data = {'bar': 'baz'}
response = self.client.post(foo_path, data, follow=True)
# Get last redirect
self.assertGreater(len(response.redirect_chain), 0)
# last_url will be something like 'http://testserver/.../'
last_url, status_code = response.redirect_chain[-1]
self.assertIn(bar_path, last_url)
self.assertEqual(status_code, 302)
# Get the exact final path from the response,
# excluding server and get params.
last_path = response.request['PATH_INFO']
self.assertEqual(bar_path, last_path)
# Note that you can also assert for redirects directly.
self.assertRedirects(response, bar_path)
Bạn đang làm gì đó sai. –