【发布时间】:2015-04-10 18:43:02
【问题描述】:
一般来说,我是 PHPSpec 和 BDD/TDD 的新手。
给定以下代码:
interface Checker
{
public function execute(array $args = array());
}
class Check
{
public $checker;
public $params = array();
public function doCheck()
{
}
}
我想指定 Check 类需要将其参数传递给 Checker,但我不确定如何做。
我的规格:
class CheckSpec extends ObjectBehavior
{
function it_should_pass_params_to_checker_on_execute(\Checker $checker)
{
$checker->execute()->willReturn(true);
$this->checker = $checker;
$this->params = array(1,2);
$this->doCheck();
$checker->execute(array(1,2))->shouldHaveBeenCalled();
}
}
当我运行规范时,在检查器类中的实现之前我得到:
9 - it should pass params to checker on execute
no calls been made that match:
Double\Checker\P1->execute(exact([1, 2]))
but expected at least one.
一旦我实施:
class Check
{
public $checker;
public $params = array();
public function doCheck()
{
$this->checker->execute($this->params);
}
}
我明白了:
9 - it should pass params to checker on execute
method call:
- execute([1, 2])
on Double\Checker\P1 was not expected, expected calls were:
- execute()
什么给了?据我所知,我按照规定实现了。
【问题讨论】: