Mockery and Laravel constructor injection

前端 未结 2 1517
独厮守ぢ
独厮守ぢ 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:50

    Problem is when you create your mock:

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

    So this:

    $mock = Mockery::mock('Myname\Myapp\Models\PersonModel')
    var_dump($mock);
    die();
    

    Will output something like this: Mockery_0_Myname_Myapp_Models_PersonModel

    But this:

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

    Will output this: Mockery\CompositeExpectation

    So try doing something like this:

    $mock = Mockery::mock('Myname\Myapp\Models\PersonModel');
    $mock->shouldReceive('find')->with('var');
    
    $this->app->instance('Myname\Myapp\Models\PersonModel', $mock);
    $repo = $this->app->make('Myname\Myapp\Repositories\PersonRepository');
    $result = $repo->testFunction('var');
    

提交回复
热议问题