Testing Exceptions
Testing Exceptions menunjukkan bagaimana menggunakan @expectedException
untuk menguji apakah Exceptions dikerjakan di dalam kode uji testing tersebut.
Contoh 2.10: Menggunakan @expectedException
annotation
class ExceptionTest extends PHPUnit_Framework_TestCase
{
/**
* @expectedException InvalidArgumentException
*/
public function testException()
{
}
}
?>
Hasil dari pengujian code diatas sebagai berikut :
phpunit ExceptionTest
Didalam hasil test tersebut terdapat error
Failed asserting that exception of type "InvalidArgumentException" is thrown.
Selain itu kita dapat menggunakan message yang lain untuk keperluan kita, dengan kombinasi dengan @expectedException
:
@expectedExceptionMessage
@expectedExceptionMessageRegExp
@expectedExceptionCode
Lihat code berikut ini, ini adalah contoh bagaimana
@expectedException
dikombinasikan dengan beberapa message yang lain
Contoh 2.11: Menggunakan pesan @expectedExceptionMessage, @expectedExceptionMessageRegExp dan @expectedExceptionCode
class ExceptionTest extends PHPUnit_Framework_TestCase
{
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Right Message
*/
public function testExceptionHasRightMessage()
{
throw new InvalidArgumentException('Some Message', 10);
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessageRegExp #Right.*#
*/
public function testExceptionMessageMatchesRegExp()
{
throw new InvalidArgumentException('Some Message', 10);
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionCode 20
*/
public function testExceptionHasRightCode()
{
throw new InvalidArgumentException('Some Message', 10);
}
}
?>