如何在PHPSpec中使用ThrowException匹配器?

时间:2012-07-30 15:52:36

标签: php tdd bdd phpspec

我在使用ExceptionMatcher时遇到问题...我的示例规范:

class DescribeBall extends \PHPSpec\Context {

private $_ball = null;

function before() {
    $this->_ball = $this->spec(new Ball);
}

function itShouldHaveStatusRolledOnRoll() {
        $this->_ball->roll();
        $this->_ball->getStatus()->should->be('Rolled');
}

function itShouldThrowException() {
    $this->_ball->getException()->should->throwException('Exception','Error');
}
}

我的示例课

class Ball {
    private $status = null;

    public function roll() {
        $this->status = 'Rolled';
    }

    public function getStatus() {
        return $this->status;
    }

    public function getException() {
        throw new Exception('Error');
    }

}

有人使用过这个匹配器吗?

$this->_ball->getException()->should->throwException('Exception','Error');

1 个答案:

答案 0 :(得分:4)

感谢我的同事们:

“我最后一次看它,它使用了闭包(除非Marcello同时改变它)它应该仍然像这样工作”:

function itShouldThrowException() { 
    $ball = $this->_ball;
    $this->spec(function() use ($ball) {
            $ball->getException();
        })->should->throwException('Exception','Error');
}