Cakephp测试登录

Elw*_*his 6 php simpletest login cakephp-1.3

我想测试登录功能,如果它正常工作,只允许有效和活跃的用户.

我的用户夹具包含:

array(
        'password' => '*emptyPasswordHash*', // empty password
        'username' => 'Lorem',
        'balance' => 0,
        'currency' => 'USD',
        'id' => 1,
        'user_group_id' => 3, //Customer
        'active' => 1,
        'hash' => 'LoremHash'
    ),
Run Code Online (Sandbox Code Playgroud)

我的测试函数如下所示:

function testLogin() {
            //test valid login
            $result = $this->testAction('/users/login', array(
                'data' => array(
                    'User' => array(
                        'username' => 'Lorem',
                        'pass' => '',
                        'remember' => true
                )),
                'method' => 'post',
                'return' => 'view'
            ));

            debug($result);
Run Code Online (Sandbox Code Playgroud)

}

登录表单具有3个输入:username,passwordremember

我已经设置$this->Auth->autoRedirect = false;UsersController::beforeFilter,我正在做一些cookie设置的东西

当我debug($this->data);UsersController::login()测试时和正常记录时显示完全相同的数据.但是,在测试登录失败时,我收到$this->Auth->loginError消息而不是登录.

如何正确测试登录操作?

dog*_*c69 1

如果你使用 cakes Auth 组件并且不破解它,你不需要......

https://github.com/cakephp/cakephp/blob/master/cake/tests/cases/libs/controller/components/auth.test.php#L545

如果你真的想的话,看看专业人士是如何做的:)