Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
phpspec-指定应将参数传递给模拟对象_Php_Phpspec - Fatal编程技术网

phpspec-指定应将参数传递给模拟对象

phpspec-指定应将参数传递给模拟对象,php,phpspec,Php,Phpspec,总的来说,我是新手 给定以下代码: interface Checker { public function execute(array $args = array()); } class Check { public $checker; public $params = array(); public function doCheck() { } } 我想指定Check类需要将其参数传递给Checker,但我不确定如何这样做 我的规格: c

总的来说,我是新手

给定以下代码:

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类中实现之前,我得到:

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()
有什么好处?据我所知,我是按照规定实施的。

我得到了:

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