2014-02-14 75 views
1

當編寫一個蛋糕php控制器用模擬對象進行測試時,位置標題似乎沒有設置。從我看到的代碼我知道的一個事實是重定向被調用正確的值。我在這裏做錯了什麼?CakePHP/PHPUnit:測試重定向 - 位置標題似乎沒有被正確設置

登錄代碼:

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

應該

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

感謝,mromagnoli,但我不記得這個問題的背景下 - 它看起來並不像一個很好的問題,其隨後所有的審查所有這些個月後 – nishantjr