0
votes

I'm trying to test a Laravel api. When I try to create a partial mock with the Eloquent Model fill method, phpunit throws an error.

Code

$mock = m::mock('App\User', [])->makePartial();
$mock->shouldReceive('fill')->once()->andReturn('ok');
$result = $mock->fill([]);
var_dump($result);

Error

PHP Fatal error:  Call to a member function __call() on a non-object
PHP Fatal error:  Uncaught exception 'Illuminate\Contracts\Container\BindingResolutionException' with message 'Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable.

I really don't know if this either a Eloquent bug, or a Mockery error.

Notes:

I temporarily solved this problem using Model::update method instead of Model::fill and then Model::save, but I still want to know how to mock the fill method.

Links

http://laravel.com/api/5.0/Illuminate/Database/Eloquent/Model.html#method_fill

2

2 Answers

1
votes

I think the mock object is created but without this method. You will need to define an expectation for the fill() method to dictate her mocked behaviour. like:

$mock->shouldReceive('fill')->once()->andReturn('ok');

hope it helps.

0
votes

You can also use a passive partial mock as:

$mock = m::mock('Model')->makePartial();
$mock->shouldReceive("fill")->once()->andReturn("ok");

In passive partial, all methods will simply defer to the parent class original methods unless a method call matches a known expectation. And it will skip the call of the unexpected fill method in Model constructor.