6

我是单元测试的新手,所以这可能是一个有点愚蠢的问题。想象一下,我们有一个简单的模型方法。

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 单元测试库是 Toast,所以我正在寻找示例,最好使用它编写。谢谢。

4

1 回答 1

10

我也在使用 toast,主要是用它来测试模型方法。为此,首先截断所有表值,插入预定义值,然后获取它。这是我在应用程序中使用的测试示例:

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加载数据库库后,您可以在测试控制器内部执行此操作。你可以把它放在自动加载中,这样它就会自动加载。

See that in my code, before the test is run, the table is truncated. After run, I will get any error that might occured. I do assert for a predefined insert and update. Using Toast to test the model will make you sure that the model's method doing exactly the task that you want it to do. Make the test that you need, and make sure you cover all the possibilities of input and output values.

于 2010-02-17T05:11:59.690 回答