Mockery and Laravel constructor injection

前端 未结 2 1515
独厮守ぢ
独厮守ぢ 2021-02-20 08:09

I am using laravel 5 with php unit to create a laravel package. I have a Repository..

namespace Myname\\Myapp\\Repositories;

use Myname\\Myapp\\Mod         


        
2条回答
  •  隐瞒了意图╮
    2021-02-20 08:51

    While Fabio gives a great answer, the issue here is really the test setup. Mockery's mock objects do comply to contracts and will pass instanceof tests and type hints in method arguments.

    The problem with the original code is that the chain of methods being called end up returning an expectation rather than a mock. We should instead create a mock first, then add expectations to that mock.

    To fix it, change this:

    $mock = Mockery::mock('Myname\Myapp\Models\PersonModel')
        ->shouldReceive('find')
        ->with('var');
    

    Into this:

    $mock = Mockery::mock('Myname\Myapp\Models\PersonModel');
    $mock->shouldReceive('find')->with('var');
    

    The variable $mock will now implement PersonModel.

    Bonus:

    Instead of 'Myname\Myapp\Models\PersonModel', use PersonModel::class. This is a lot more IDE-friendly and will help you when refactoring code later on.

提交回复
热议问题