2014-01-22 17 views
1

我正在使用CakePHP的測試框架來測試我的控制器。我有一個註銷功能,會在用戶使用該網站時創建各種Cookie。我正在嘗試讀取所述cookie以確定測試是否應該通過,即測試cookie是否正確過期。我確定cookie組件已正確實例化,但我無法從應該在那裏的cookie讀取任何值。這是構成我運行測試代碼:無法從Cakephp讀取cookie數據phpunit test

public function testLogout() { 
    // setup the cookie component 
    $collection = new ComponentCollection(); 
    $this->Cookie = new CookieComponent($collection); 

    $result = $this->testAction('/users/logout'); 
    $cookie_name = Configure::read('tech_cookie_name'); 
    $cookie_data = $this->Cookie->read($cookie_name); 

    debug($cookie_name); 

    // cookie data is returning as NULL but I'm expecting some type of value. 
    debug($cookie_data); 
    debug($result); 
    exit; 
} 

我認識到,退出是早期查殺測試,但我用它來看看是否有什麼是發送從cookie回來。我不確定爲什麼我不能從我知道的cookie中讀取任何數據。有誰知道爲什麼會這樣,或者有一個解決方案,以便在單元測試中如何正確讀取cookie。

回答

1

在某些情況下,你不能從routes.php中讀取Configure :: read(),這不是一個好習慣。它將在localhost中工作,但不在現場。嘗試正確配置您的會話。 通過從AppController和您當前的Controller(UserController)調用您的會話,然後您應該能夠在您的測試操作中看到它。

$this->Session->write('Test.tech_cookie_name', 'tech_cookie_name'); 

,那麼你應該能夠這樣來閱讀::

$this->Session->read('Test.tech_cookie_name'); 

public $components = array('Session', 'RequestHandler', 'Cookie', ...); 
如果你寫你會喜歡這個