功能测试类依赖于夹具中创建的对象引用。但是,引用的 id 与实体管理器返回的对象的 id 属性不同。下面的测试演示了这个问题。
笔记:
$this->setReference(...)
使用 时的错误与使用public const ...
和 时的错误相同$this->addReference(...)
。
测试中使用的对象引用似乎是非营利实体的下一个可用 ID。
在更一般的测试类中观察到错误后创建测试类。
无论在运行测试类之前是否加载了装置,错误都是相同的。
该应用程序使用 Symfony 5.1.2,并更新了所有依赖项。
测试类:
namespace App\Tests\Controller;
use Liip\TestFixturesBundle\Test\FixturesTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class ReferenceTest extends WebTestCase
{
use FixturesTrait;
public function setup(): void {
$this->client = $this->createClient();
$this->fixtures = $this->loadFixtures([
'App\DataFixtures\Test\OptionsFixture',
'App\DataFixtures\Test\NonprofitFixture',
'App\DataFixtures\Test\OpportunityFixture',
'App\DataFixtures\Test\UserFixture',
])
->getReferenceRepository();
$this->client->followRedirects();
$kernel = self::bootKernel();
$this->entityManager = $kernel->getContainer()
->get('doctrine')
->getManager('test');
}
public function testNonprofitReference() {
$npo = $this->entityManager->getRepository(\App\Entity\Nonprofit::class)
->findOneBy(['orgname' => 'Marmot Fund']);
$nId = $npo->getId();
$id = $this->fixtures->getReference('npo')->getId();
$this->assertEquals($nId, $id, 'Reference incorrect');
}
}
测试结果:
Reference incorrect
Failed asserting that 4 matches expected 1.
ABOUTYOU