1

一般来说,我是PHPSpecBDD/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();
    }
}

当我运行规范时,在 Checker Class 中实现之前,我得到:

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()

是什么赋予了?据我所知,我按规定实施。

4

1 回答 1

0

I got it:

class CheckSpec extends ObjectBehavior
{    
    function it_should_pass_params_to_checker_on_execute(\Checker $checker)
    {
        // I should've created my mock with the expected parameters
        $checker->execute(array(1,2))->willReturn(true);
        // Like so ------>^^^^^^^^^^^      
        $this->checker = $checker;
        $this->params = array(1,2);
        $this->doCheck();
        $checker->execute(array(1,2))->shouldHaveBeenCalled();
    }
}
于 2015-04-10T19:05:42.343 回答