【问题标题】:Laravel - phpUnit test while using DoctrineLaravel - 使用 Doctrine 时的 phpUnit 测试
【发布时间】:2017-11-17 12:50:04
【问题描述】:

我使用 Laravel + Doctrine(而不是 Eloquent)+ Angular Routing 创建了一个项目。我决定使用 php 单元测试来测试 API(控制器、存储库)实际上我在测试一个简单的方法时遇到了错误。这是我的代码:

DoctorineRestaurantRepositoryTest.php:

    class RestaurantTest extends TestCase
     {
         private $DoctrineRepository;
         public  function setUp() {
             $this->DoctrineRepository = DoctrineRestaurantRepository::class;
           }
        /** @test */
        public function validator()
        {
            $this->DoctrineRepository->setTestVariable(3);
            $this->assertEquals($this->DoctrineRepository->getTestVariable(), 3);
        }
      .
      .
      .
    }

我的存储库文件:(DoctrineRestaurantRepository.php)

class DoctrineRestaurantRepository extends DoctrineBaseRepository
{

    private $testVariable = 0;

    /**
     * @return int
     */
    public function getTestVariable()
    {
        return $this->testVariable;
    }

    /**
     * @param int $testVariable
     */
    public function setTestVariable($testVariable)
    {
        $this->testVariable = $testVariable;
    }

  .
  .
  .
}

我进行了测试,它给出了一个错误:

Call to a member function setTestVariable() on string

有什么解决办法吗?

【问题讨论】:

  • 你在 __construct 中注入了 DoctrineRepository 吗?我认为你需要检查它是否被注入。
  • $this->DoctrineRepository 是在哪里创建的?听起来您引用的类名如 DoctrineRepository::class。
  • @btl 我已经编辑了我的问题。
  • 您没有实例化 DoctrineRestaurantRepository... 您需要调用 new DoctrineRestaurantRepository() 或将其绑定到应用服务容器以供以后解析。 $this->app->bind(DoctrineRestaurantRepository::class, new DoctrineRestaurantRepository());然后你可以稍后使用它 $this->app->make(DoctrineRestaurantRepository::class)

标签: php laravel doctrine-orm phpunit


【解决方案1】:

你需要在方法中定义类变量或者注入类。

解决方案 1

向类变量添加新对象

use DoctrineRestaurantRepository;

class RestaurantTest extends TestCase
{
    private $DoctrineRepository;

    public function __construct()
    {
        $this->DoctrineRepository = new DoctrineRestaurantRepository;
    }
    /** @test */
    public function validator()
    {
        $this->DoctrineRepository->setTestVariable(3);
        $this->assertEquals($this->DoctrineRepository->getTestVariable(), 3);
    }
  .
  .
  .
}

解决方案 2

使用依赖注入

use DoctrineRestaurantRepository;

class RestaurantTest extends TestCase
{
    private $DoctrineRepository;

    public function __construct(DoctrineRestaurantRepository $repository)
    {
        $this->DoctrineRepository = $repository;
    }
    /** @test */
    public function validator()
    {
        $this->DoctrineRepository->setTestVariable(3);
        $this->assertEquals($this->DoctrineRepository->getTestVariable(), 3);
    }
  .
  .
  .
}

【讨论】:

  • 方法 1 的结果:PHP 致命错误:未捕获的类型错误:传递给 DoctrineORM\DoctrineBaseRepository::__construct() 的参数 1 必须是 Doctrine\ORM\EntityManager 的一个实例,没有给出,在 DoctorineRestaurantRepositoryTest 中调用。 php 在第 18 行并在 /DoctrineORM/DoctrineBaseRepository.php:22 中定义
  • DoctrineBaseRepository 中的 __construct 看起来像这样:public function __construct(EntityManager $em)
  • 那么在创建新的 DoctrineRestaurantRepository 实例时必须传递所有依赖项的实例
猜你喜欢
  • 1970-01-01
  • 2016-06-04
  • 2014-09-28
  • 2017-11-29
  • 1970-01-01
  • 2023-03-06
  • 2015-09-26
  • 2023-03-24
  • 2014-09-11
相关资源
最近更新 更多