错误使用嘲弄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.

1个回答

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');
doujiao2443
doujiao2443 使用进程隔离运行测试的注释,你应该在测试期间调用Mockery :: mock
大约 5 年之前 回复
doulai1910
doulai1910 这个过程“Mockery :: mock('something')”,在开始测试之前我总是需要这样做吗? 它不能在运行时完成?
大约 5 年之前 回复
douyueqing1530
douyueqing1530 我是单元测试的新手。 如何在一个孤立的过程中运行测试? 我真的可以测试静力学方法吗?
大约 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问