简体   繁体   English

使用嘲笑Laravel进行错误测试

[英]Error make test with mockery laravel

I'm trying to test a method of a class in my laravel project with mockery. 我试图用嘲笑来测试我的laravel项目中的类方法。
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. 但是,当我尝试测试phpunit时说我的接口类(正在我需要测试的方法中使用)不是可实例化的。
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. 您在HelperRS上调用的方法是静态方法,而您正在创建该类的模拟实例,而实际上并未对其进行任何操作。 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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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