CakePHP / PHPUnit:测试重定向 - 似乎没有正确设置位置标头

时间:2014-02-14 07:07:33

标签: cakephp phpunit

当写一个蛋糕php控制器测试使用模拟对象时,Location标头似乎没有设置。通过查看我知道的代码,我知道正在使用正确的值调用重定向。我在这里做错了什么?

登录代码:

    if (!$this->Auth->login()) {
        ...
        echo "Redirect reached.\n";
        $this->redirect('/users/login');
    }

测试:

public function testLoginFailedRedirectsToLogin() {
    $users = $this->generate('Users', array(
        'components' => array('Session'=>array('setFlash'))));
    $users->Session
        ->expects($this->once())
        ->method('setFlash');
    $users->response
        ->expects($this->once())
        ->method('header')
        ->with('Location', '/users/login');

    $data = array('Users' => array(
            'username' => 'bad-user',
            'password' => 'infinet',
            'remember_me' => false,
        ));
    $this->testAction('/users/login',
        array('data' => $data, 'method' => 'post'));
    var_dump($this->headers['Location']);
    $this->assertEqual($this->headers['Location'], '/users/login');
}       

输出:

Redirect reached.
string(74) "http://localhost/home/nishant/projects/atp/1/.build/tmp/27784/app/Console/"
...

1) LoginTest::testLoginFailedRedirectsToLogin
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'/users/login'
+'http://localhost/home/nishant/projects/atp/1/.build/tmp/27784/app/Console/

如果我删除了assertEqual,重定向的期望也会失败。

1 个答案:

答案 0 :(得分:-1)

应该是

return $this->redirect('/users/login');