我也在使用 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.