2

I am trying to test a method that I have using PhpSpec and Prophecy. I am having a little trouble getting it to work, though. I don't want to use actual values for the mock, so I have used Argument::any(), but it seems to be wanting to execute the actual call inside the method. I thought that this was what mocks were to prevent against?

My Class:

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

My Test:

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

And the error I am getting back:

error: Argument 1 passed to Double\Illuminate\Validation\Factory\P2::make() must be of the type array, object given, called in /Users/will/development/personal/blog/app/Blog/Service/Laravel/CreatePostValidator.php on line 32 and defined in /Users/will/development/personal/blog/vendor/phpspec/prophecy/src/Prophecy/Doubler/Generator/ClassCreator.php(49) :
eval()'d code line 10

will
  • 1,491
  • 1
  • 19
  • 28

1 Answers1

2

This one is not an expectation, so just pass [] there:

$this->with([]);
Jarek Tkaczyk
  • 78,987
  • 25
  • 159
  • 157