猿问

功能测试中的夹具对象引用不正确

功能测试类依赖于夹具中创建的对象引用。但是,引用的 id 与实体管理器返回的对象的 id 属性不同。下面的测试演示了这个问题。

笔记:

  1. $this->setReference(...)使用 时的错误与使用public const ...和 时的错误相同$this->addReference(...)

  2. 测试中使用的对象引用似乎是非营利实体的下一个可用 ID。

  3. 在更一般的测试类中观察到错误后创建测试类。

  4. 无论在运行测试类之前是否加载了装置,错误都是相同的。

  5. 该应用程序使用 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.


智慧大石
浏览 119回答 1
1回答

ABOUTYOU

答案是,引用在功能测试中没有地位。它们的使用实际上是单击链接或执行其他操作的快捷方式。更好的测试是使用爬虫来模仿动作。
随时随地看视频慕课网APP
我要回答