2012-10-01 14 views
2

我正在爲登錄系統編寫單元測試 - 但與Zend_Session_Namespace對象交互時遇到困難。我使用PHPUnit框架的擴展Zend_Test(ZF 1.10,PHPUnit的3.4.5)Zend_Session_Namespace單元測試

測試

​​

電流控制器/動作代碼

public function validateAction() 
{ 
    $loginSession = new Zend_Session_Namespace("login"); 

    if (!$this->buildForm()->isValid($this->getRequest()->getPost())) { 
    $loginSession->messages["error"][] = "Please enter your username and password"; 
    } 

    $loginSession->messages["error"][] = "Could not contact the authentication server"; 

    $this->getResponse() 
    ->setHttpResponseCode(303) 
    ->setHeader("Location", "/auth/login"); 
} 

我的單元測試是在失敗$this->assertObjectHasAttribute("messages", $loginSession); - 但我期望它在郵件的實際內容上失敗。

我在PHPUnit的引導這些行:

Zend_Session::$_unitTestEnabled = true; 
Zend_Session::start(); 

如何確認會話消息是否設置正確?

回答

0

總共有捂臉時刻,這個問題今天重新看:

我的Zend_Session_Namespace的「消息」屬性實際上是一個神奇的屬性(使用__get和__set訪問),而不是身份通過反射(這就是PHPUnit在調用assertObjectHasAttribute時如何測試該屬性的存在性)。

從測試中刪除斷言允許它繼續,並且現在我的測試在預期點失敗並且不會過早。