douhao2026 2014-11-27 01:11
浏览 82
已采纳

错误使用嘲弄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条回答 默认 最新

  • dongyuan1902 2014-11-27 07:57
    关注

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?