는 최근에 나는 phpunit을 테스트를 작성하기 시작했다. 그리고 이것은 내 모델 코드입니다 (CodeIgniter 3를 사용했습니다). 나 자신에 의해 SQL을 쓰기 때문에은 100 %로 코드 커버리지를 만든 방법
Account_model.php는
class Account_model extends CI_Model
{
...
public function select_by_seq($seq = '', $select_columns = [])
{
try {
$bind = [':a_seq' => $seq];
// check $select_colums is exist in table
if ($this->check_column($select_columns) === false)
{
throw new Exception('columns illegal', 201);
}
...
$sql = "select ....
from {$this->db->dbprefix('account')}
where a_seq = :a_seq";
$query = $this->db->query($sql, $bind);
// ===== this always not runing. =====
if ($query === false)
{
// ===== this always not runing. =====
throw new Exception('sql errors', 301);
}
else
{
return $query->result_array();
}
}
catch (Exception $error)
{
// set error log
$this->set_error_log($error->getCode() . $error->getMessage());
}
return false;
}
}
이 Account_model_test.php
class Account_model_test extends TestCase
{
public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
$CI =& get_instance();
}
public function setUp()
{
$this->resetInstance();
loader('model', 'account_model');
$this->obj = $this->CI->account_model;
}
public function test_select_by_seq()
{
$result = $this->obj->select_by_seq(
$seq = '20160830'
);
$this->assertCount(1, $result);
}
public function test_select_by_seq_with_illegal_column()
{
$result = $this->obj->select_by_seq(
$seq = '20160830',
$select_columns = ['illegal']
);
$this->assertFalse($result);
}
...
}
내 테스트 입니다. 내 PHPUnit 테스트를 설립하여 이것을 if ($query === false)
에 적용 할 수 없습니다. 그리고 나서 제 코드 범위는 100 %를 달성하지 못했습니다. 이 문제는 100 %가 단위 테스트에서 매우 중요하다고 생각하게합니까? 또는 모델 코드를 수정해야합니까? 고마워.
당신은 필기 시험의 목적을 놓쳤다. – axiac