donglizuo8892 2015-07-27 12:10 采纳率: 0%
浏览 68
已采纳

用phpunit测试异常

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?

  • 写回答

1条回答 默认 最新

  • drwiupraq047311240 2015-07-27 12:19
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?