简体   繁体   English

使用 RequestFactory() 时测试消息

[英]Testing messages when using RequestFactory()

I am testing a class based view using mock to raise an exception.我正在使用 mock 测试基于 class 的视图以引发异常。 On exception, a message should be created and then a redirect executed.在异常情况下,应该创建一条消息,然后执行重定向。 Whilst I am able to test that the redirect has been executed, I am unable as yet to retrieve the message to be able to verify it.虽然我能够测试重定向是否已执行,但我还无法检索消息以验证它。

view看法

CustomUser = get_user_model()

class SignUpView(FormView):
    template_name = 'accounts/signup.html'
    form_class = SignUpForm

    def form_valid(self, form):

        try:
            self.user = CustomUser.objects.filter(email=form.cleaned_data['email']).first()

            if not self.user:

                self.user = CustomUser.objects.create_user(email=form.cleaned_data['email'],
                                                           full_name=form.cleaned_data['full_name'],
                                                           password=form.cleaned_data['password'],
                                                           is_verified=False
                                                           )
            else:
                if self.user.is_verified:

                    self.send_reminder()

                    return super().form_valid(form)

            self.send_code()
        except:
            messages.error(self.request, _('Something went wrong, please try to register again'))
            return redirect(reverse('accounts:signup'))

        return super().form_valid(form)

My test so far:到目前为止我的测试:

class SignUpViewTest(TestCase):

    def setUp(self):
        self.factory = RequestFactory()

    def test_database_fail(self):
        with patch.object(CustomUserManager, 'create_user') as mock_method:
            mock_method.side_effect = Exception(ValueError)

            view = SignUpView.as_view()
            url = reverse('accounts:signup')
            data = {'email': 'test@test.com', 'full_name': 'Test Tester', 'password': 'Abnm1234'}
            request = self.factory.post(url, data)
            setattr(request, 'session', 'session')
            messages = FallbackStorage(request)
            request._messages = messages

            response = view(request)

            self.assertEqual(response.status_code, 302)
            self.assertEqual(response.url, '/accounts/signup/')
           

My question is, how do I retrieve the message so that I can make an assertEqual against the message: 'Something went wrong, please try to register again'?我的问题是,如何检索消息以便我可以对消息进行 assertEqual:“出现问题,请尝试重新注册”?

After digging a little deeper the message(s) can be found here:深入挖掘后,可以在此处找到消息:

request._messages._queued_messages[0]

And therefore the assertEqual would be:因此 assertEqual 将是:

self.assertEqual(str(request._messages._queued_messages[0]), 'Something went wrong, please try to register again')

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

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