使用 phpunit 测试异常
testing exceptions with phpunit
当我知道会抛出错误时,我正在尝试测试一个函数。该函数如下所示:
function testSetAdsData_dataIsNull(){
$dataArr = null;
$fixture = new AdGroup();
try{
$fixture->setAdsData($dataArr);
} catch (Exception $e){
$this->assertEquals($e->getCode(), 2);
}
$this->assertEmpty($fixture->ads);
$this->assertEmpty($fixture->adIds);
}
现在我正在尝试使用 phpunit 异常断言方法来替换 try catch 部分,但我不知道该怎么做。
我读了很多书,包括这个 post PHPUnit assert that an exception was thrown? 但我真的不明白它应该如何实现。
我试过这样的事情:
/**
* @expectedException dataIsNull
*/
function testSetAdsData_dataIsNull(){
$dataArr = null;
$fixture = new AdGroup();
$this->setExpectedException('dataIsNull');
$fixture->setAdsData($dataArr);
$this->assertEmpty($fixture->ads);
$this->assertEmpty($fixture->adIds);
}
but obviously it didn't work and i got this error:
1) adGroupTest::testSetAdsData_dataIsNull
ReflectionException: Class dataIsNull does not exist
我做错了什么,如果抛出异常我该如何断言?
我通常在这种情况下使用 @expectedException
注释。见 all exception-related annotations here:
/**
* @expectedException \Exception
* @expectedExceptionCode 2
*/
function testSetAdsData_dataIsNull()
{
$dataArr = null;
$fixture = new AdGroup();
$fixture->setAdsData($dataArr);
}
检查 $fixture->ads
是否真的为 null 并没有真正添加到这里,您可以在实际触发异常的调用之前添加这些断言:
$this->assertNull($fixture->ads);
$fixture->setAdsData($dataArr);//throws exception
您正在 单元 测试。这个测试有一个明确的目的:它确保在给定的情况下抛出异常。如果是,那么测试到此结束。
不过,如果您想保留那些 assertEmpty
电话,您可以这样做:
try {
$fixture->setAdsData($dataArr);
$e = null;
} cathc (Exception $e) {}
$this->assertEmpty($fixture->ads);
$this->assertEmpty($fixture->adIds);
if (!$e instanceof \Exception) {
//if the exception is not thát important:
$this->markTestIncomplete('No Exception thrown');
//do other stuff here... possibly
$this->fail('The exception was not thrown');
}
throw $e;//throw exception a bit later
另一种方法是手动调用 $this->setExpectedException
as explained here。由于我们似乎不知道 know/care 异常消息会是什么样子,所以我将使用 setExpectedExceptionRegExp
方法:
$fixture = new AdGroup();
$this->setExpectedExceptionRegExp(
//exception class, message regex, exception code
'Exception', '/.*/'. 2
);
$fixture->setAdsData(null);//passing null seems to be what you're doing anyway
当我知道会抛出错误时,我正在尝试测试一个函数。该函数如下所示:
function testSetAdsData_dataIsNull(){
$dataArr = null;
$fixture = new AdGroup();
try{
$fixture->setAdsData($dataArr);
} catch (Exception $e){
$this->assertEquals($e->getCode(), 2);
}
$this->assertEmpty($fixture->ads);
$this->assertEmpty($fixture->adIds);
}
现在我正在尝试使用 phpunit 异常断言方法来替换 try catch 部分,但我不知道该怎么做。 我读了很多书,包括这个 post PHPUnit assert that an exception was thrown? 但我真的不明白它应该如何实现。
我试过这样的事情:
/**
* @expectedException dataIsNull
*/
function testSetAdsData_dataIsNull(){
$dataArr = null;
$fixture = new AdGroup();
$this->setExpectedException('dataIsNull');
$fixture->setAdsData($dataArr);
$this->assertEmpty($fixture->ads);
$this->assertEmpty($fixture->adIds);
}
but obviously it didn't work and i got this error:
1) adGroupTest::testSetAdsData_dataIsNull
ReflectionException: Class dataIsNull does not exist
我做错了什么,如果抛出异常我该如何断言?
我通常在这种情况下使用 @expectedException
注释。见 all exception-related annotations here:
/**
* @expectedException \Exception
* @expectedExceptionCode 2
*/
function testSetAdsData_dataIsNull()
{
$dataArr = null;
$fixture = new AdGroup();
$fixture->setAdsData($dataArr);
}
检查 $fixture->ads
是否真的为 null 并没有真正添加到这里,您可以在实际触发异常的调用之前添加这些断言:
$this->assertNull($fixture->ads);
$fixture->setAdsData($dataArr);//throws exception
您正在 单元 测试。这个测试有一个明确的目的:它确保在给定的情况下抛出异常。如果是,那么测试到此结束。
不过,如果您想保留那些 assertEmpty
电话,您可以这样做:
try {
$fixture->setAdsData($dataArr);
$e = null;
} cathc (Exception $e) {}
$this->assertEmpty($fixture->ads);
$this->assertEmpty($fixture->adIds);
if (!$e instanceof \Exception) {
//if the exception is not thát important:
$this->markTestIncomplete('No Exception thrown');
//do other stuff here... possibly
$this->fail('The exception was not thrown');
}
throw $e;//throw exception a bit later
另一种方法是手动调用 $this->setExpectedException
as explained here。由于我们似乎不知道 know/care 异常消息会是什么样子,所以我将使用 setExpectedExceptionRegExp
方法:
$fixture = new AdGroup();
$this->setExpectedExceptionRegExp(
//exception class, message regex, exception code
'Exception', '/.*/'. 2
);
$fixture->setAdsData(null);//passing null seems to be what you're doing anyway