duanlu1876
2017-06-15 06:48
浏览 35
已采纳

Laravel - 使用Doctrine时的phpUnit测试

I created a project with Laravel + Doctrine (instead of Eloquent) + Angular Routing . I decided to use php unit test for testing API (Controllers , Repositories) Actually I am geting an error while testing a simple method. Here is my code :

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);
        }
      .
      .
      .
    }

my repository file: (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;
    }

  .
  .
  .
}

I ran test and it gave an error :

Call to a member function setTestVariable() on string

Any suggestion to fix it?

图片转代码服务由CSDN问答提供 功能建议

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

DoctorineRestaurantRepositoryTest.php:

  class RestaurantTest扩展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

 类DoctrineRestaurantRepository扩展了DoctrineBaseRepository 
 {
 
 private $ testVariable = 0; 
 
 / ** 
 * @return int 
 * / 
公共函数getTestVariable()
 {
 
返回$ this  - > testVariable; 
} 
 
 / ** 
 * @param int $ testVariable 
 * / 
  public function setTestVariable($ testVariable)
 {
 $ this-> testVariable = $ testVariable; 
} 
 
。
。
。
} 
   \  n 
 

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

 调用字符串
   \上的成员函数setTestVariable()  n 
 

有任何修复建议吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongre6227 2017-06-15 08:50
    已采纳

    You need to define the class variable or inject the class in the method.

    Solution 1

    adding new object to the class variable

    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);
        }
      .
      .
      .
    }
    

    Solution 2

    using dependency injection

    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);
        }
      .
      .
      .
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题