我有一个注册视图,可以注册新用户,将其登录,然后将其重定向到主页。当我使用unittest测试此视图时,我得到一个AssertionError 200!= 302
views.py:
def register(request):
if request.method =='POST':
form = RegistrationForm(request.POST)
if form.is_valid():
user=form.save()
username = request.POST.get('username')
password = request.POST.get('password1')
login(request, user)
return redirect(reverse('home:home'))
else:
form = RegistrationForm()
args = {'form': form}
return render(request, 'accounts/reg_form.html', args)
test_views.py:
class TestViews(TestCase):
def setUp(self):
self.client = Client()
self.burial=User.objects.create_user(
username='burial',
password='secret'
)
def test_registration_view(self):
url = reverse('accounts:register')
response = self.client.post(url, {
'username': self.burial.username,
'password': self.burial.password
})
self.client.login(username=self.burial.username,
password=self.burial.password)
self.assertEquals(self.burial.username, 'burial')
self.assertEquals(response.status_code, 302)
我收到AssertionError 200!= 302
谢谢您的帮助。