afo*_*tcu 4 php testing phpunit doctrine doctrine-orm
我正在尝试模拟 Doctrine 存储库和 entityManager,但 PHPUnit 一直告诉我:
1) CommonUserTest::testGetUserById 尝试配置方法“findBy”,因为它不存在,没有被指定,是最终的,或者是静态的,所以无法配置
这是片段:
<?php
use \Domain\User as User;
use PHPUnit\Framework\TestCase;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\EntityManager;
class CommonUserTest extends PHPUnit_Framework_TestCase
{
public function testGetUserById()
{
// mock the repository so it returns the mock of the user (just a random string)
$repositoryMock = $this
->getMockBuilder(EntityRepository::class)
->disableOriginalConstructor()
->getMock();
$repositoryMock->expects($this->any())
->method('findBy')
->willReturn('asdasd');
// mock the EntityManager to return the mock of the repository
$entityManager = $this
->getMockBuilder(EntityManager::class)
->disableOriginalConstructor()
->getMock();
$entityManager->expects($this->any())
->method('getRepository')
->willReturn($repositoryMock);
// test the user method
$userRequest = new User($entityManager);
$this->assertEquals('asdasd', $userRequest->getUserById(1));
}
}
Run Code Online (Sandbox Code Playgroud)
有什么帮助吗?我尝试了此代码的一些变体,但无法通过此特定错误。
谢谢。
小智 7
您使用的是哪个版本的 PHPUnit?
无论如何,请检查这个可能的解决方案:
$repositoryMock = $this
->getMockBuilder(EntityRepository::class)
->setMethods(['findBy'])
->disableOriginalConstructor()
->getMock();
Run Code Online (Sandbox Code Playgroud)
应该管用。
| 归档时间: |
|
| 查看次数: |
4230 次 |
| 最近记录: |