嘲弄PhpSpec和Prophecy的问题

时间:2014-12-19 16:38:19

标签: php mocking tdd phpspec

我正在尝试使用PhpSpecProphecy测试我使用的方法。不过,我在使用它时遇到了一些麻烦。我不想使用模拟的实际值,所以我使用了Argument::any(),但它似乎想要在方法中执行实际调用。我以为这就是嘲讽要防止的?

我的班级:

class CreatePostValidator
{

    protected $validator;

    protected $data;

    protected $rules = [
        'title' => 'required',
        'body' => 'required',
        'author' => 'required',
        'slug' => 'required'
    ];


    public function __construct(Factory $validator)
    {
        $this->validator = $validator;
    }

    public function with($data)
    {
        $this->data = $data;
    }

    public function passes()
    {
        $validator = $this->validator->make($this->data, $this->rules);

        if ($validator->fails())
        {
            return false;
        }

        return true;
    }
}

我的测试:

class CreatePostValidatorSpec extends ObjectBehavior
{
    function let(Factory $factory)
    {
        $this->beConstructedWith($factory);
    }

    function it_is_initializable()
    {
        $this->shouldHaveType('Blog\Service\Laravel\CreatePostValidator');
    }

    function it_should_return_false_if_validator_fails(
        Factory $factory, Validator $validator)
    {
        $factory->make(Argument::any(), Argument::any())->willReturn($validator);
        $validator->fails()->willReturn(true);

        $this->with(Argument::any());
        $this->passes()->shouldReturn(false);
    }
}

我得到的错误:

  

错误:参数1传递给   Double \ Illuminate \ Validation \ Factory \ P2 :: make()必须是类型   数组,给定对象,调用   /Users/will/development/personal/blog/app/Blog/Service/Laravel/CreatePostValidator.php   在第32行并在中定义   /Users/will/development/personal/blog/vendor/phpspec/prophecy/src/Prophecy/Doubler/Generator/ClassCreator.php(49)   :
  eval()' d代码行10

1 个答案:

答案 0 :(得分:2)

这不是期望,所以只需通过[]

$this->with([]);