小编Sad*_*ari的帖子

symfony 5 中的功能测试

这是我的 symfony 项目,我正在其中练习功能测试,当我测试我的功能时出现这样的错误。

在此输入图像描述

在这里,发生我的错误的代码部分:\

<?php

namespace App\Tests;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use App\Entity\Category;

class AdminControllerCategoriesTest extends WebTestCase
{
public function setUp():void
{
    parent::setUp();
    $this->client = static::createClient();

    $this->entityManager = $this->client->getContainer()->get('doctrine.orm.entity_manager');

    $this->entityManager->beginTransaction();

    $this->entityManager->getConnection()->setAutoCommit(false);
}

public function tearDown():void
{
    parent::tearDown();
    $this->entityManager->rollback();
    $this->entityManager->close();
    $this->entityManager = null; //avoid memory leaks
}

public function testTextOnPage()
{
    $crawler = $this->client->request('GET', '/admin/categories');
    $this->assertSame('Categories list', $crawler->filter('h2')->text());
    $this->assertContains('Electronics', $this->client->getResponse()->getContent());
}

public function testNumberOfItems()
{
    $crawler = $this->client->request('GET', '/admin/categories');
    $this->assertCount(21, $crawler->filter('option'));
}
}
Run Code Online (Sandbox Code Playgroud)

在这里,我的 .env,我有数据库连接:

    # In all environments, the following files are …
Run Code Online (Sandbox Code Playgroud)

php testing functional-testing symfony doctrine-orm

5
推荐指数
2
解决办法
1966
查看次数

标签 统计

doctrine-orm ×1

functional-testing ×1

php ×1

symfony ×1

testing ×1