CakePHP 2.3 - 单元测试用户登录 [英] CakePHP 2.3 - Unit testing User Login

查看:138
本文介绍了CakePHP 2.3 - 单元测试用户登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以为我必须问这里一些帮助我的问题。我花了整整一个晚上。我在 UsersController 中有一个登录方法,例如:

I thought I have to ask here some help to my problem. I've spend whole evening with this. I have a login method in UsersController like this:

public function login() {

        if ( $this->request->is( 'post' ) ) {
            if ( $this->Auth->login() ) {
                $this->redirect( array( 'controller' => 'reservations', 'action' => 'index' ) );
            } else {
                $this->Session->setFlash( __( 'Login error.' ), 'flashError' );
            }
        }
    }

PHPUnit ,因此我可以确保只有有效的用户才能登录→在成功登录后,他们将被重定向到特定页面。这是 UsersControllerTest 类中的 testLogin 方法:

I trying to test this with PHPUnit, so I can be sure that only valid users can login → after a successful login they will be redirected to a specific page. Here's my testLogin method in UsersControllerTest class:

function testLogin() {

        $UsersController = $this->generate( 'Users', array(
                'components' => array(
                    'Auth' => array( 'user' )
                ),
            )
        );

        $UsersController->Auth->expects( $this->any() )
        ->method( 'user' )
        ->with( 'id' )
        ->will( $this->returnValue( 2 ) );

        $data = array( 'User' => array(
                'student_number' => 1111111,
                'password' => 'qwerty'
            ) );

        //$UsersController->Auth->login( $data['User'] );

        $this->testAction( '/users/login', array( 'data' => $data, 'method' => 'get' ) );
        $url = parse_url( $this->headers['Location'] );
        $this->assertEquals( $url['path'], '/reservations' );
    }



我还在学习使用CakePHP进行单元测试的基础知识。我收到此错误:

I am still learning the basics of unit testing with CakePHP. I get this error:

PHPUNIT_FRAMEWORK_ERROR_NOTICE
Undefined index: Location
Test case: UsersControllerTest(testLogin)

我不知道是什么原因...我的测试方法有什么问题, ?

I have no idea what causes this... What's wrong with my test method and how it should be written?

谢谢!

推荐答案

代码:

function testLogin() {

        //mock user
        $this->Users = $this->generate( 'Users', array(
                'components' => array(
                    'Security' => array( '_validatePost' ),
                )
            ) );

        //create user data array with valid info
        $data = array();
        $data['User']['student_number'] = 1234567;
        $data['User']['password'] = '[valid password here]';

        //test login action
        $result = $this->testAction( "/users/login", array(
                "method" => "post",
                "return" => "contents",
                "data" => $data
            )
        );

        $foo[] = $this->view;
        //debug($foo);

        //test successful login
        $this->assertNotNull( $this->headers['Location'] );
        $this->assertContains( 'reservations', $this->headers['Location'] );
        $this->assertNotContains( '"/users/login" id="UserLoginForm"', $foo );

        //logout mocked user
        $this->Users->Auth->logout();
    }

这篇关于CakePHP 2.3 - 单元测试用户登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆