0

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

笔记:

  1. $this->setReference(...)使用时的错误与使用public const ...and时的错误相同$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.

NonprofitFixture(其他灯具可能不相关):

namespace App\DataFixtures\Test;

use App\Entity\Nonprofit;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Bundle\FixturesBundle\ORMFixtureInterface;

class NonprofitFixture extends AbstractFixture implements OrderedFixtureInterface, ORMFixtureInterface
{

    public const NPO_REFERENCE = 'npo';

    public function load(ObjectManager $manager) {
        $npo = new Nonprofit();
        $npo->setOrgname('Marmot Fund');
        $npo->setEin('123456789');
        $npo->setActive(false);
//        $this->setReference('npo', $npo);
        $this->addReference(self::NPO_REFERENCE, $npo);

        $npo1 = new Nonprofit();
        $npo1->setOrgname('Turkey Fund');
        $npo1->setEin('321654978');
        $npo1->setActive(true);
        $npo1->setWebsite('http://turkeysRUs.bogus.info');

        $npo3 = new Nonprofit();
        $npo3->setOrgname('Talk Trash Fund');
        $npo3->setEin('978654321');
        $npo3->setActive(true);
        $npo3->setWebsite('http://ttrash.bogus.info');

        $staff = $this->getReference(UserFixture::STAFF_REFERENCE);
        $npo->setStaff($staff);
        
        $opp = $this->getReference(OpportunityFixture::OPP_REFERENCE);
        $opp1 = $this->getReference(OpportunityFixture::OPP1_REFERENCE);
        $npo1->addOpportunity($opp);
        $npo3->addOpportunity($opp1);

        $manager->persist($npo);
        $manager->persist($npo1);
        $manager->persist($npo3);

        $manager->flush();
    }

    public function getOrder() {
        return 5; // the order in which fixtures will be loaded
    }

}

framework.yaml 摘录:

liip_test_fixtures:
    keep_database_and_schema: true
    cache_db:
        sqlite: liip_test_fixtures.services_database_backup.sqlite

dama_doctrine_test_bundle.yaml:

dama_doctrine_test:
    enable_static_connection: true
    enable_static_meta_data_cache: true
    enable_static_query_cache: true

从 app.db 导出 csv:

"id","orgName"
"1","Marmot Fund"
"2","Turkey Fund"
"3","Talk Trash Fund"
4

1 回答 1

0

答案是,引用在功能测试中没有位置。它们的使用实际上是点击链接或采取其他行动的捷径。更好的测试是使用爬虫来模仿动作。

于 2020-07-16T23:12:17.937 回答