1

i am trying to test a function when i know error i going to be thrown. the function looks like this:

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);
}

Now i am trying to use the phpunit exceptions assertions methods to replace the try catch part but i can't figure out how to do that. i did lots of reading including this post PHPUnit assert that an exception was thrown? but i couldnt really understand how it shuold be implemented.

i tried something like this:

/**
 * @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

what am i doing wrong and how exactly can i assert if exception was thrown plz?

Community
  • 1
  • 1
Donoven Rally
  • 1,670
  • 2
  • 17
  • 34

1 Answers1

1

I generally use the @expectedException annotations for just such cases. See all exception-related annotations here:

/**
 * @expectedException \Exception
 * @expectedExceptionCode 2
 */
function testSetAdsData_dataIsNull()
{
    $dataArr = null;
    $fixture = new AdGroup();

    $fixture->setAdsData($dataArr);
}

Checking that $fixture->ads is really null doesn't really add up here, you can add these asserts prior to the call that actually triggers an exception:

$this->assertNull($fixture->ads);
$fixture->setAdsData($dataArr);//throws exception

You're unit testing. This test serves a clear purpose: it makes sure an exception is thrown in a given situation. If it does, then that's where the test ends.

Still, if you want to keep those assertEmpty calls, you could do this:

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

An alternative approach would be to call $this->setExpectedException manually as explained here. Since we don't seem to know/care what the exception message will look like, I'm going to use the setExpectedExceptionRegExp method:

$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
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • thx for the helping @Elias Van Ootegem...so just to make sure, in order to keep executing tests i need to use try catch anyway?? – Donoven Rally Jul 27 '15 at 12:57
  • @DonovenRally: Not at all. If you don't add those `$this->assertEmpty()` calls _after_ the call that throws the exception, the first snippet is all you need. No try-catch anywhere, and the test _should_ be successful – Elias Van Ootegem Jul 27 '15 at 13:12