【问题标题】:How to user ThrowException matcher in PHPSpec?如何在 PHPSpec 中使用 ThrowException 匹配器?
【发布时间】:2012-07-30 15:52:35
【问题描述】:

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

【问题讨论】:

    标签: php tdd bdd phpspec


    【解决方案1】:

    感谢我的同事:

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多