2010-02-16 3 views
6

단위 테스트를 처음 사용하기 때문에 조금 어리석은 질문 일 수 있습니다. 우리는 간단한 모델 방법을 가지고 있다고 상상해보십시오.Codeigniter 단위 테스트 모델

public function get_all_users($uid = false, $params = array()){ 
    $users = array(); 
    if(empty($uid) && empty($params)){return $users;} 
    $this->db->from('users u'); 
    if($uid){ 
     $this->db->where('u.id',(int)$id); 
    } 
    if(!empty($params)){ 
     if(isset($params['is_active']){ 
      $this->db->where('u.status ', 'active'); 
     } 
     if(isset($params['something_else']){ // some more filter actions} 
    } 
    $q = $this->db->get(); 
    if($q->num_rows()){ 
     foreach($q->result_array() as $user){ 
      $users[$user['id']] = $user; 
     } 
    } 
    $q->free_result(); 
    return $users; 
} 

질문은 _good 테스트를 작성하는 방법입니다. UPD : CI를위한 최고의 단위 테스트 라이브러리는 토스트입니다. 예를 들어 찾고 싶으므로이를 사용하여 작성하는 것이 좋습니다. 감사합니다. .

답변

10

저는 토스트 (토스트)도 사용하고 있으며, 주로 모델 방법을 테스트하는 데 사용합니다. 이를 수행하려면 먼저 모든 테이블 값을 절단하고 미리 정의 된 값을 삽입 한 다음 가져옵니다. 이것은 내가 내 응용 프로그램에 사용되는 시험의 예는 다음과 같습니다

class Jobads_tests extends Toast 
{ 
    function Jobads_tests() 
    { 
    parent::Toast(__FILE__); 
    // Load any models, libraries etc. you need here 
    $this->load->model('jobads_draft_model'); 
    $this->load->model('jobads_model'); 
    } 

    /** 
    * OPTIONAL; Anything in this function will be run before each test 
    * Good for doing cleanup: resetting sessions, renewing objects, etc. 
    */ 
    function _pre() 
    { 
    $this->adodb->Execute("TRUNCATE TABLE `jobads_draft`"); 
    } 

    /** 
    * OPTIONAL; Anything in this function will be run after each test 
    * I use it for setting $this->message = $this->My_model->getError(); 
    */ 
    function _post() 
    { 
    $this->message = $this->jobads_draft_model->display_errors(' ', '<br/>'); 
    $this->message .= $this->jobads_model->display_errors(' ', '<br/>'); 
    } 

    /* TESTS BELOW */ 
    function test_insert_to_draft() 
    { 
    //default data 
    $user_id = 1; 

    //test insert 
    $data = array(
     'user_id' => $user_id, 
     'country' => 'ID', 
     'contract_start_date' => strtotime("+1 day"), 
     'contract_end_date' => strtotime("+1 week"), 
     'last_update' => time() 
    ); 
    $jobads_draft_id = $this->jobads_draft_model->insert_data($data); 
    $this->_assert_equals($jobads_draft_id, 1); 

    //test update 
    $data = array(
     'jobs_detail' => 'jobs_detail', 
     'last_update' => time() 
    ); 
    $update_result = $this->jobads_draft_model->update_data($jobads_draft_id, $data); 
    $this->_assert_true($update_result); 

    //test insert_from_draft 
    $payment_data = array(
     'activation_date' => date('Y-m-d', strtotime("+1 day")), 
     'duration_amount' => '3', 
     'duration_unit' => 'weeks', 
     'payment_status' => 'paid', 
     'total_charge' => 123.45 
    ); 
    $insert_result = $this->jobads_model->insert_from_draft($jobads_draft_id, $payment_data); 
    $this->_assert_true($insert_result); 

    //draft now must be empty 
    $this->_assert_false($this->jobads_draft_model->get_current_jobads_draft($user_id)); 

    } 
} 

내 응용 프로그램에서 ADODB를 사용하고 있지만, 그것과 혼동되지 않습니다. 데이터베이스 라이브러리를로드 한 후 $this->db을 테스트 컨트롤러에서 수행 할 수 있습니다. 자동로드되므로 자동로드됩니다.

내 코드에서 테스트를 실행하기 전에 테이블이 잘 리도록하십시오. 실행 후 오류가 발생할 수 있습니다. 나는 미리 정의 된 삽입과 갱신을 주장한다. Toast를 사용하여 모델을 테스트하면 모델의 메서드가 원하는 작업을 정확히 수행하는지 확인할 수 있습니다. 필요한 테스트를 수행하고 입력 및 출력 값의 모든 가능성을 확인하십시오.

+0

멋진데, 왜 모든 테스트를 단일 테스트 기능 내에 두어야하는지 궁금합니다. 개별 함수에 넣지 않는 이유 - assert 문이 실패하면 bugtrack을 쉽게 수행 할 수 있다고 가정합니다. –

+0

위의 테스트에서 내 애플리케이션의 실제 사례를 반영합니다. 그래서 한 가지 시험에서 2 가지 '행동'이있는 것입니다. 이렇게해도 실패하면 첫 번째 또는 두 번째 작업이 실패합니다. 필요에 맞게 원하는대로 만들 수 있습니다. –