【发布时间】: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