TestCase帮助。 AssertionError:200!= 302

问题描述 投票:0回答:1

嗨,我正在尝试使用以下代码运行一些测试

视图

@login_required
def compose(request):
    """Form to compose a new message"""
    template_name = 'messaging/compose.html'
    form_class = MessageForm
    if request.method == 'POST':
        form = form_class(request.POST)
        if form.is_valid():
            form.save(sender=request.user)
            messages.info(request, 'Message sent successfully', extra_tags='sent')
            success_url = reverse_lazy('messaging:messages_outbox')
            return redirect(success_url)
    else:
        form = form_class()
    return render(request, template_name, {'form': form})

TestCase

class TestMessageViews(TestCase):
    def setUp(self):
        super().setUp()
        self.user1 = get_user_model().objects.create(username='user1')
        self.user2 = get_user_model().objects.create(username='user2')
        self.form = create(Message, {'sender': self.user1,
                                     'recipient': self.user2,
                                     'subject': 'test_subject',
                                     'body': 'test_body'})
        self.client = Client()
    def test_compose_message(self):
        """Test to make sure our compose is redirecting us"""
        self.client.force_login(user=self.user1)
        response = self.client.post(reverse('messaging:messages_compose'), {
            'recipient': self.user2,
            'subject': self.form.subject,
            'body': self.form.body
        })
        self.assertEqual(response.status_code, 302)

我将获得200的状态码,这应该将我们重定向到应该是302的发件箱?

django django-forms django-views django-testing django-tests
1个回答
热门问题
推荐问题
最新问题