首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用夹具加载后删除夹具

使用夹具加载后删除夹具
EN

Stack Overflow用户
提问于 2014-04-03 16:31:02
回答 1查看 1.5K关注 0票数 4

我正在为我的代码做一些测试,我得到了我的第一个“停止”,因为我不知道如何向前推进。在我的setUp()函数中,我加载了夹具:

代码语言:javascript
复制
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();
}

但是,我已经删除了创建的数据,因为我已经对坏情况进行了测试(当非实体被返回时):

代码语言:javascript
复制
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中的,所以测试失败。对此有什么建议吗?你的是怎么解决的?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-03 16:42:10

做你要做的事没有简单的方法。通常所做的是在执行测试之前和之后截断数据库,这样您就拥有了一个真正干净和孤立的环境。

引用这篇好文章(http://blog.sznapka.pl/fully-isolated-tests-in-symfony2/ )

代码语言:javascript
复制
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://github.com/liip/LiipFunctionalTestBundle

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22843668

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档