Php 当进行单元测试时,当调用的函数进行SQL查询时,您会做什么?
我有一个如下所示的函数:Php 当进行单元测试时,当调用的函数进行SQL查询时,您会做什么?,php,sql,unit-testing,phpunit,Php,Sql,Unit Testing,Phpunit,我有一个如下所示的函数: function putThis() /* * ********************************* <summary> Function: putThis Purpose: Puts this in the database Inputs: None Outputs: None Notes: </summary> * ********************************* */ {
function putThis()
/* * *********************************
<summary>
Function: putThis
Purpose: Puts this in the database
Inputs: None
Outputs: None
Notes:
</summary>
* ********************************* */ {
$err = $this->checkAll();
if ($err == '') {
if ($this->isNew == true) {//create procedure
$err = createSupportAccount($this);
} else {//update
$err = updateSupportAccount($this);
}
}
return $err;
}
函数putThis()
/* * *********************************
功能:推杆
目的:将其放入数据库
投入:无
产出:无
笔记:
* ********************************* */ {
$err=$this->checkAll();
如果($err==''){
如果($this->isNew==true){//create过程
$err=createSupportAccount($this);
}else{//更新
$err=updateSupportAccount($this);
}
}
返回$err;
}
我正在尝试进行单元测试,以确保putThis()函数正常工作。在这种情况下,对象被传递给一个函数,该函数使用该对象进行数据库查询。我肯定可以用虚拟数据填充对象,但是这意味着函数将返回错误。如果我使用无效的输入数据测试用例,返回错误不是问题,但是如果我想测试函数是否正常工作,如何正确测试它?如果您已经抽象了数据库层,那么您可以模拟it@MarkBaker模拟它的哪个方面?如果您的代码实例化了数据库连接,然后将该对象注入到类中,您可以模拟数据库连接对象,并将其注入到类中