如何进行cakephp的单元测试

时间:2013-05-28 20:34:15

标签: unit-testing cakephp

我正在尝试围绕cakephp的单元测试,我想知道是否有人可以提供一些关于如何为特定控制器方法编写测试的输入:

public function paymentmethod() {

    $this->layout='dashboard';
    $billingaddressInfo = $this->Dashboard->find('all',array('fields' => array('Address_book_id','first_name','last_name'),'conditions' => array('Dashboard.customer_id' => $this->Session->read('customer_id'),'address_type'=>'1')));
    $billingaddress = array();
    if(is_array($billingaddressInfo) && count($billingaddressInfo) > 0) {
        foreach($billingaddressInfo as $key=>$value) {
            $billingaddress[$value['Dashboard']['Address_book_id']] = $value['Dashboard']['first_name'].' '.$value['Dashboard']['last_name'];
        }   
    }
    $this->set('billingaddress',$billingaddress);

    $fullbillingaddress = $this->Dashboard->find('all',array('fields' => array('Address_book_id','customer_id','first_name','last_name','address_line_1','address_line_2','city','state','country','zipcode'),
                                    'conditions' => array('Dashboard.customer_id' =>$this->Session->read('customer_id'))));

    $this->set('fullbillingaddress',$fullbillingaddress);   
    $shippingaddress = $this->Dashboard->find('list',array('fields' => array('first_name'),'conditions' => array('Dashboard.customer_id' => $this->Session->read('customer_id'),'address_type'=>'2')));

    $this->set('shippingaddress',$shippingaddress);

    $this->loadModel('Paymentmethod');

    if(!empty ($this->request->data)) {     
        $getpaymentform = $this->request->data['Paymentmethod'];            
        $getpaymentform['card_number'] = $this->encryptCard($getpaymentform['card_number']);            
        if($this->request->data['Paymentmethod']['is_default']==1) {                
            $this->Paymentmethod->updateAll(array('is_default'=>'0'),array('Paymentmethod.customer_id' =>$this->Session->read('customer_id')));
        }
        $this->Paymentmethod->save($getpaymentform);

    }
    $paymentdata = $this->Paymentmethod->find('all',array('conditions' => array('Paymentmethod.customer_id' =>$this->Session->read('customer_id'))));
    $this->set('paymentdata',$paymentdata);
    $this->render();

    if(!empty ($this->request->data)) {
        $this->redirect(array('action'=>'paymentmethod')); 
    }           
}

我真的在寻找有关要测试的方法的哪些部分以及要断言的内容的建议,并且我不是在寻找任何人来编写代码,而只是对您将如何接近的经验评估这个。我是新手,非常感谢一些意见。

1 个答案:

答案 0 :(得分:1)

分解代码的各个部分以使其可单独测试

将“查找”操作移至模型将是一个良好的开端,这样您就可以测试操作的各个部分。

例如;

class Dashboard extends AppModdel
{

    public function getBillingAddress($customerId)
    {
        $billingaddressInfo = $this->find('all',
            array(
                'fields' => array(
                    'Address_book_id',
                    'first_name',
                    'last_name',
                ),
                'conditions' => array(
                    'Dashboard.customer_id' => $customerId,
                    'Dashboard.address_type' => 1,
                ),
            )
        );

        $billingaddress = array();
        if(is_array($billingaddressInfo) && count($billingaddressInfo) > 0) {
            foreach($billingaddressInfo as $key=>$value) {
                $billingaddress[$value['Dashboard']['Address_book_id']] = $value['Dashboard']['first_name'].' '.$value['Dashboard']['last_name'];
            }   
        }

        return $billingaddress;
    }
}

(注意:使用virtualField可能会更容易做到这一点,但这不是这个问题的主题)

而且,在您的控制器内,只需:

$customerId = $this->Session->read('customer_id');
$billingAddress = $this->Dashboard->getBillingAddress($customerId);

不仅会将代码移动到您的Model中,而且代码更清晰(参见我的示例),您还可以将getBillingAddress()方法与其他代码分开进行测试。

要进一步了解单元测试的方式/内容,请务必查看CakePHP本身的源代码。在lib / Cake / test目录中,您可以找到Cake本身的单元测试,其中包含有关如何测试应用程序某些部分的有价值信息(例如,如何测试模型,控制器,组件等)

相关问题