gpt4 book ai didi

database - 在 Symfony 2.6 上用许多请求测试隔离

转载 作者:行者123 更新时间:2023-11-28 21:29:29 25 4
gpt4 key购买 nike

我的项目使用 Symfony 2.6。我试图隔离我的测试,以免对我的数据库进行任何更改。

我设法将我的测试与一个请求隔离开来。但是当它们很多时,它不起作用,我无法找到原因。有什么想法吗?

这是我的测试 Controller 代码:

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class RequestControllerTest extends WebTestCase{

protected $client;
protected $entityManager;

protected function setUp(){
parent::setUp();
$this->client = static::createClient();
$this->entityManager = $this->client->getContainer()->get('doctrine')->getManager();
$this->entityManager->beginTransaction();
}

protected function tearDown(){
parent::tearDown();
$this->entityManager->rollback();
$this->entityManager->close();
}

// This test is working just fine : the request isn't deleted from database at the end
public function testIsolationOK(){
$this->client->request('DELETE', self::WEB_SERVICES_EXISTING_REQUEST_URL);
$this->client->request('GET', self::WEB_SERVICES_EXISTING_REQUEST_URL);
}

// But this one isn't working : the request is deleted from database at the end
public function testIsolationNOK(){
$this->client->request('GET', self::WEB_SERVICES_EXISTING_REQUEST_URL);
$this->client->request('DELETE', self::WEB_SERVICES_EXISTING_REQUEST_URL);
}

}

最佳答案

老实说,进行这种测试最简单和最安全的方法是创建一个新的数据库用于测试目的,并在 config_test.yml 中指定其配置。

使用这种方法,您将确保您的真实数据库没有被测试修改的危险,并且还会使您在测试时更轻松。

我通常使用这种方法,但我不知道它是否是您想要的。

Documentation

希望对您有所帮助。

关于database - 在 Symfony 2.6 上用许多请求测试隔离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28249846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com