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 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。