代码之家  ›  专栏  ›  技术社区  ›  geoB

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

  •  0
  • geoB  · 技术社区  · 6 年前

    笔记:

    1. 使用时错误相同 $this->setReference(...) 使用 public const ... 和 $this->addReference(...) .
    2. 非营利实体的可用id。
    3. 测试类是在一个更通用的测试类中观察到错误后创建的。
    4. 应用程序更新了Symfony的所有依赖项。

    测试等级:

    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.
    

    非营利固定装置(其他装置可能不相关):

    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
        }
    
    }
    

    框架.yaml节选:

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

    大马丘学说测试_包扎.yaml:

    dama_doctrine_test:
        enable_static_connection: true
        enable_static_meta_data_cache: true
        enable_static_query_cache: true
    

    csv导出自应用数据库:

    "id","orgName"
    "1","Marmot Fund"
    "2","Turkey Fund"
    "3","Talk Trash Fund"
    
    0 回复  |  直到 6 年前
        1
  •  0
  •   geoB    6 年前

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

    推荐文章