我正在为我的代码做一些测试,我得到了我的第一个“停止”,因为我不知道如何向前推进。在我的setUp()函数中,我加载了夹具:
public function setUp() {
static::$kernel = static::createKernel();
static::$kernel->boot();
$this->em = static::$kernel->getContainer()->get('doctrine')->getManager();
$this->user = $this->createUser();
$fix = new MetaDetailGroupFixtures();
$fix->load($this->em);
parent::setUp();
}但是,我已经删除了创建的数据,因为我已经对坏情况进行了测试(当非实体被返回时):
public function testListMetaDetailGroupFailAction() {
$client = static::createClient();
$this->logIn($client, $this->user);
$route = $client->getContainer()->get('router')->generate('meta-detail-group-list', array('parent_id' => 20000), false);
$client->request("GET", $route);
$decoded = json_decode($client->getResponse()->getContent(), true);
$this->assertCount(0, $decoded['entities']);
$this->assertArrayHasKey('success', $decoded);
$this->assertJsonStringEqualsJsonString(json_encode(array("success" => false, "message" => "No existen grupos de metadetalles de productos creados")), $client->getResponse()->getContent());
$this->assertSame(200, $client->getResponse()->getStatusCode());
$this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type'));
$this->assertNotEmpty($client->getResponse()->getContent());
}由于记录是在安装程序中创建并保留在DB中的,所以测试失败。对此有什么建议吗?你的是怎么解决的?
发布于 2014-04-03 16:42:10
做你要做的事没有简单的方法。通常所做的是在执行测试之前和之后截断数据库,这样您就拥有了一个真正干净和孤立的环境。
引用这篇好文章(http://blog.sznapka.pl/fully-isolated-tests-in-symfony2/ )
public function setUp()
{
$kernel = new \AppKernel("test", true);
$kernel->boot();
$this->_application = new \Symfony\Bundle\FrameworkBundle\Console\Application($kernel);
$this->_application->setAutoExit(false);
$this->runConsole("doctrine:schema:drop", array("--force" => true));
$this->runConsole("doctrine:schema:create");
$this->runConsole("doctrine:fixtures:load", array("--fixtures" => __DIR__ . "/../DataFixtures"));
}如您所见,该解决方案利用Doctrine的Symfony2命令来实现隔离状态。我喜欢使用一个包,它正好解决了这个问题,让您可以使用FunctionalTest基类和许多其他特性。看看这个:
https://stackoverflow.com/questions/22843668
复制相似问题