简体   繁体   中英

Error make test with mockery laravel

I'm trying to test a method of a class in my laravel project with mockery.
However, when I attempt to test the phpunit says a my interface class (which is being used in the method I need to test) is not instantiable.
What's wrong?

My test class

class HelperRSTest extends TestCase {

    public function tearDown()
    {
      Mockery::close();
    }

    public function test_mockery()
    {
        // $mock = Mockery::mock('HelperRS');
        // $mock->shouldReceive('getRecibosLocacao')->once()->andReturn('mocked');

        $result = HelperRS::getRecibosLocacao(1228);
        var_dump($result);
    }

}

My target class to test

class HelperRS extends \BaseController {

    public static function getRecibosLocacao($id_locacao){

        $pagamentos = App::make('PagamentoRepositoryInterface');

        $locacao = Locacao::find($id_locacao);
        $pagamento = $pagamentos->getByVendaByTipo($locacao->cod_locacao, 'l');

        dd($pagamento);

    }

}

The error:

1) HelperRSTest::test_mockery
Illuminate\Container\BindingResolutionException: Target [PagamentoRepositoryInterface] is not instantiable.

The method you are calling on HelperRS is a static method, whereas you are creating a mocked instance of that class and not actually doing anything with it. You can achieve this with mockery, though it is not recommended and will require you to run the test in process isolation.

$mock = Mockery::mock('alias:HelperRS');
$mock->shouldReceive('getRecibosLocacao')->once()->andReturn('mocked');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM