2011-05-11 171 views
1

這是一個冗長的問題,但我花了幾個小時尋找和嘗試不同的事情,但還沒有得到它解決,所以這是我的最後希望。單元測試自定義Zend_Form元素

我創建了一個自定義的Zend_Form_Element,我從這裏得到了代碼:http://www.zendcasts.com/ajaxify-your-zend_form-validation-with-jquery/2010/04/所有的代碼都可以在該網站上找到,但基本上我創建了一個具有多個輸入元素的窗體,它返回一個值。

做對得到的網頁上查看源文件,表單元素看起來是這樣

<input size="3" maxlength="3" id="phone-areanum" name="phone[areanum]" value="" type="text"> 
<input size="3" maxlength="3" id="phone-geonum" name="phone[geonum]" value="" type="text"> 
<input size="4" maxlength="4" id="phone-localnum" name="phone[localnum]" value="" type="text"> 

這裏是單元測試,我覺得應該工作,但不是

public function testValidDataRedirectsToAppointmentTimePage() 
{ 
    $phone = array('areanum'=>'480', 'geonum'=>'123', 'localnum'=>'5678'); 
    $this->request->setMethod('post') 
        ->setPost(array(
        'phone' => $phone, 
        'name' => 'Smith' 
       )); 
    $this->dispatch('/sign-in'); 


    // assert that user was redirected to current-patient page 
    $this->assertRedirectTo('/current-patient'); 
} 

我如果用戶輸入他們的電話號碼和姓名到字段中,他們將被重定向到正確的頁面。當我將Zend_Debug::dump($this->getResponse()->getBody());卡入單元測試函數時,我能夠確定我在post中設置的值導致錯誤,並且頁面未正確加載。 我已經試過了$手機其他值:

$phone = array('480', '123', '5678'); 
$phone = array(480, 123, 5678); 
$phone = '480-123-5678'; 
$phone = '48'; 

想不出還有什麼沒有嘗試?

更新:添加控制器動作

public function phoneAction() 
{ 
    $this->view->title = "Please Sign In"; 
    $this->view->headTitle("Sign In"); 
    $form = new Application_Form_Phone(); 

    $this->view->form = $form; 

    if($this->getRequest()->isPost()) { 
     if($form->isValid($this->getRequest()->getPost())) { 
      $phone = $form->getValue('phone'); 
      $name = $form->getValue('name'); 

      // function to get user's id from their info 
      $this->_session->user_id = $this->_getUserId($phone, $name); 

      return $this->_redirect('/current-patient'); 
     } 
    } 
} 

class Application_Form_Phone extends Zend_Dojo_Form 
{ 

    public function init() 
    { 
     $this->addElementPrefixPath('Grabby_Form_Validate', 'Grabby/Form/Validate', 'validate'); 
     $this->setName('phoneform'); 
     $this->setMethod('post'); 

     $phone = new Grabby_Form_Element_Phone('phone'); 
     $phone->addValidator(new Grabby_Form_Validate_Phone()); 
     $this->addElement($phone); 

     $this->addElement('ValidationTextBox', 'name', array(
      'required' => true, 
      'label' => 'Last Name', 
      'trim' => true, 
      'promptMessage' =>'Enter your last name', 
      'InvalidMessage' => 'Last name required', 
     )); 

     $this->addElement('SubmitButton', 'submitbutton', array(
      'label' => 'Submit', 
      'ignore' => true, 
     )); 
    } 
} 

正如你可以看到有沒有多的事情。代碼工作正常,只是試圖單元測試,所以我可以「證明」它的工作原理。

+0

請問您可以發佈控制器/表單代碼嗎?謝謝 – opHASnoNAME 2011-05-12 05:01:05

+2

從技術上講,這可能不是單元測試_element_,而是一起測試元素,窗體和控制器。也許更狹窄的單元測試關注元素本身?有關示例,請參見[form element unit-tests](http://framework.zend.com/code/listing.php?repname=Zend+Framework&path=%2Ftrunk%2Ftests%2FZend%2FForm%2FElement%2F&#a99e5f5c78a10fd004ba3f5b1f5c641c9)。 – 2011-05-12 09:30:39

+0

@DavidWeinraub你是對的。我試圖一起測試元素,窗體和控制器。我對單元測試很陌生,所以對我最有「意義」。感謝您的鏈接,看到其他單元測試一定會對我有所幫助。 – 2011-05-12 23:20:47

回答

0

你需要的可能是沿着這些路線的東西:

$phone = array(
    'areanum' => 480, 
    'geonum' => 123, 
    'localnum' => 5678 
); 

雖然創建最終的組合值,後仍發送它分成幾部分。