How to unit-test Ausnahmen mit PHPUnit?

Ich bin nicht immer, wie unit-Tests Exceptions mit PHPUnit.

Bitte sehen Sie sich meine Methode mit der Ausnahme:

    public function getPhone($html, $tag = 'OFF', $indicative, $number_lenght) {

        //.. code

        if ($tag <> 'OFF') {

            $html = $doc[$tag]->text(); //Apanho apenas o texto dentro da TAG
                if (empty($html)) {
                    throw new Exception("Nao foi possivel apanhar qualquer texto dentro da TAG, Metodo em causa: getPhone()");
                }               
        }

        //.. code
    }

Und jetzt meine PHPUnit Test:

<?php

require_once '../Scrap.php';

class ScrapTest extends PHPUnit_Framework_TestCase
{

    protected $scrap;

    //Setup function to instantiate de object to $this->scrap
    protected function setUp()
    {
        $this->scrap = new Scrap;
    }

    /**
    * @covers Scrap::getPhone
    * @expectedException Exception
    *
    */
    public function testGetPhone() {

        //Variables1
        $array_static1 = Array(0 => 218559372, 1 => 927555929, 2 => 213456789, 3 => 912345678);
        $phone_list1   = '</div>A Front para<br /><br /><br /><br /><br /><br />-Apoio;<br />-Criação;<br />-Campanhas;<br />-Promoções<br /><br /><br />CONDIÇÕES:<br /><br />Local de Trabalho: Es<br />Folgas: Mistas<br /><br /><br /><br />ordem 500€<br /><br /><br /><br />Mínimos:<br /><br />- Conhecimentos;<br />- Ensino ;<br />-INGLÊS.<br /><br /><br /><br />Candidaturas: <br />[email protected]<br />218559372 | 927 555 929 | <br />RH<br />Rua C. Sal. 40<br />1000-000 Lisboa<br /><br /><br />+351 21 3456789 | (351) 912345678';

        //Variables2
        $array_static2 = Array(0 => 'NA');
        $phone_list2   = "";

        //.. more tests

        //Test Exception, Tag not found
        if (TRUE) {

            //Bloco try/catch para confirmar que aqui lança excepção
            try {            
                    $this->scrap->getPhone($phone_list1, 'hr', '351', '9');        
                }         
            catch (Exception $expected) {
                    return;        
                }         

            $this->fail('An expected exception has not been raised.');  
        }



    }
}
?>

Wenn ich den test habe ich "Fehler":

1) ScrapTest::testGetPhone
Expected exception Exception

FAILURES!
Tests: 1, Assertions: 5, Failures: 1.

Ausnahme wirft, aber ich will nicht zu bekommen, scheitern in der PHPUnit, Wenn die Ausnahme erhöhen, ich will den test OK.

Können Sie mir einige Hinweise?

Beste Grüße,

InformationsquelleAutor André | 2011-01-10
Schreibe einen Kommentar