如何断言用户已在django.test.TestCase中注销?

时间:2019-07-16 00:32:19

标签: django

我是Django的新手,现在正在努力编写测试代码。

在一个测试案例中,我想检查用户是否成功注销。 那我怎么写断言?

下面是很小的示例代码。

from django.test import TestCase


class TestLogin(TestCase):
    fixtures = ['myuserdata.json']

    @classmethod
    def setUpClass(cls):
        super().setUpClass()

    def setUp(self):
        self.USER_ADMIN = {
            'email': "foo1@bar.com",
            'password': "foobar1234",
        }
        self.USER_PLAIN = {
            'email': "foo2@bar.com",
            'password': "foobar1234",
        }

    def test_login_success(self):
        login_result = self.client.login(
            email=self.USER_ADMIN['email'],
            password=self.USER_ADMIN['password'],
        )
        self.assertTrue(login_result)

        login_result = self.client.login(
            email=self.USER_PLAIN['email'],
            password=self.USER_PLAIN['password'],
        )
        self.assertTrue(login_result)

        self.client.logout()
        # self.assertFoobar(???)

    def tearDown(self):
        pass

    #...

1 个答案:

答案 0 :(得分:0)

您可以使用您的注销网址测试注销。

SELECT 
    mag.id, 
    COALESCE(subs.subscriber_count, 0) AS subscribers,
    COALESCE(subs.mysub, 0) AS my_subscription
FROM magazines mag
LEFT JOIN (
    SELECT magazine_id, COUNT(*) AS subscriber_count,
        MAX(CASE WHEN subscriber_id = 1 THEN 1 ELSE 0 END) AS mysub
    FROM subscriptions GROUP BY magazine_id) subs ON subs.magazine_id = mag.id
ORDER BY mag.id ASC