Php 错误:在null上调用成员函数create(),单元测试(mockry)

Php 错误:在null上调用成员函数create(),单元测试(mockry),php,laravel,unit-testing,phpunit,mockery,Php,Laravel,Unit Testing,Phpunit,Mockery,我正在尝试单元测试具有“create()”方法的“AdminService”类,并且在构造函数中我需要一个依赖项,它是AdminRepository 我使用mockry来模拟存储库,并将此模拟传递给AdminService\u contsruct(),但当我运行测试时,我得到了一个错误:error:调用null上的成员函数create() 这是我试图测试的管理员服务: 我跟随这篇文章: 测试代码为: /** @test */ public function it_can_create_Ad

我正在尝试单元测试具有“create()”方法的“AdminService”类,并且在构造函数中我需要一个依赖项,它是AdminRepository

我使用mockry来模拟存储库,并将此模拟传递给AdminService\u contsruct(),但当我运行测试时,我得到了一个错误:error:调用null上的成员函数create()

这是我试图测试的管理员服务: 我跟随这篇文章: 测试代码为:

/** @test */
   public function it_can_create_Admin(){

      $data=[
         "first_name"=>"lama",
         "last_name"=>"sonmez",
         "email"=>"lamasonmez@gmail.com",
         "password"=>"secret"
      ];


      $repo_mock = Mockery::mock(AdminRepository::class);
      $repo_mock->shouldReceive('create')->once()->with($data)
          ->andReturn('anything');
      $admin_service = new AdminService($repo_mock);
      $admin = $admin_service->create($data);
      $this->assertInstanceOf(Admin::class,$admin);

   }   
我不知道这里出了什么问题,请帮忙

当我dd($repo_mock)给我模拟时,我的意思是它不是空的
但当我dd($admin_service)时,我得到了


哦,我明白了为什么会发生这种情况,这是因为AdminRepository和AdminService中构造函数的下划线符号,它应该在构造键之前有两个下划线,像这样的\uu构造,而不是像这样的\u构造

/** @test */
   public function it_can_create_Admin(){

      $data=[
         "first_name"=>"lama",
         "last_name"=>"sonmez",
         "email"=>"lamasonmez@gmail.com",
         "password"=>"secret"
      ];


      $repo_mock = Mockery::mock(AdminRepository::class);
      $repo_mock->shouldReceive('create')->once()->with($data)
          ->andReturn('anything');
      $admin_service = new AdminService($repo_mock);
      $admin = $admin_service->create($data);
      $this->assertInstanceOf(Admin::class,$admin);

   }   
App\Services\AdminService^ {#4106
  #admin_repo: null
}