I am testing this function using PHPUnit:
public 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);
}
I was told this is not the right way and that I should use @expectedException
, so I started reading the manual and some answers here including this popular one:
PHPUnit assert that an exception was thrown?
But after quite a lot of reading I still didn't understand what's the benefit of using this other than my initial approach. I guess it has something to do with the fact I didn't really understand how to use it. I tried different ways, but none of them actually worked.
I realise I can add a line like this in the code :
throw new MyException('Some Error Msg', 2);
but then I need to use try catch as well (or so I understood) so what is the benefit?