PHPUnit-测试最后一个插入ID

PHPUnit-测试最后一个插入ID,php,phpunit,Php,Phpunit,有人能告诉我如何在我的PhpUnit测试中涵盖以下例外情况吗 $DB->Insert(db query here...); $lastInsertID = $DB->InsertID(); if (!$lastInsertID) { throw new Exception('Unable to insert new record'); } 提前感谢您可以模拟$DB->Insert返回false我认为您的Insert应该抛出异常而不是沉默。@sectus,请用一个例子详细

有人能告诉我如何在我的PhpUnit测试中涵盖以下例外情况吗

$DB->Insert(db query here...);

$lastInsertID = $DB->InsertID();

if (!$lastInsertID) {
    throw new Exception('Unable to insert new record');
}

提前感谢

您可以模拟
$DB->Insert
返回
false

我认为您的
Insert
应该抛出异常而不是沉默。@sectus,请用一个例子详细说明一下?谢谢,请举例说明?ThanksJust检查了文档,但我仍然认为您不能在方法中模拟其中一行并使其返回false
$DB->Insert
在本例中,模拟$DB实例,即insertID()方法。您应该有类似$db=$this->getDB();。在这种情况下,模拟当前对象,即getDB()方法。它应该返回mocked DbInstance,使用mocked insertID()方法。这是我到目前为止所做的,但是代码覆盖率仍然不包括主代码中的
if(!$lastInsertID){throw new Exception('cannot insert new record');}
。请协助我如何覆盖这些线路?
$stub=$this->getMock('DbClass',array('InsertID');$stub->expects($this->once())->method('InsertID')->will($this->returnValue(false));$this->assertTrue($stub->InsertID())你覆盖了错误的类。应该类似于`/*@expectedException*/函数testInsert(){$class->setDB($stubbedDB);//应该模拟InsertID以返回false$class->newRecord();//问题代码}`最好按照@sectus所说的做
DbClass::InsertID()
应该检查查询是否成功执行,如果没有,则抛出异常。否则,您必须在太多的位置检查
InsertID()