0

我在使用 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');
4

1 回答 1

4

感谢我的同事:

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

function itShouldThrowException() { 
    $ball = $this->_ball;
    $this->spec(function() use ($ball) {
            $ball->getException();
        })->should->throwException('Exception','Error');
}
于 2012-07-31T08:39:30.380 回答