web2py - 单元测试@auth.requires_login()

web2py - Unit testing @auth.requires_login()

全部,

我已经为我的 web2py 应用程序创建了一个单元测试。

我主要关注了这个切片:http://www.web2pyslices.com/slice/show/1465/unittesting-doctesting-and-userinterface-testing

我可以通过测试注册和登录用户。

但是我无法解决测试 auth.requires_login() 装饰器的问题。

我 运行 测试让用户登录,我可以在 auth_events 和 auth_users 中看到对测试数据库的结果更改,但尝试打开索引函数会导致重定向。

当我 运行 移除带有 @auth 装饰器的测试并放置一个简单的 if 语句来检查用户是否存在时,即我登录了一个用户。这表明用户存在​​并且 auth 对象对函数可见。

任何帮助都会很棒。

下面是单元测试的设置。我犯的错误是我在哪里执行execfile命令。

class TestBase(unittest.TestCase):

    def setUp(self):

        global auth, request, session, response, db

        self._application = 'contacts'
        self._controller = 'default'

        self._pyfile = os.path.join(request.env.web2py_path, request.folder, 'controllers', self._controller + '.py')

        self.db_link = 'postgres://......._test'

        db = test_helpers.copy_db(globals(), db_link=self.db_link)
        db.commit()

        current.request = Request(globals())

        current.session = Session()

        current.request.application = self._application
        current.request.controller = self._controller

        auth = Auth(globals(), db)

        auth.define_tables()

        db(db.contacts.id > 0).delete()  # Clear the database
        db.commit()

        # clear all users from auth tables and commit changes
        db(db.auth_user.id > 0).delete()  # Clear the database
        db.commit()

        # execute the controller file, passing in the env globals.
        execfile(self._pyfile, globals())

跟进:

此设置工作正常我可以注册,并在控制器文件中输入@auth 保护功能。

但是现在的问题是我无法调用 auth.logout() 并成功注销。我调试了它,似乎 auth 对象直到重定向后才更新。有什么办法可以解决这个问题吗??