2014-04-03 40 views
1

我正在爲我的代碼進行一些測試,我得到了我的第一個「STOP」,因爲我不知道如何向前移動。見我setUp()功能我加載夾具:使用Fixtures加載後刪除燈具

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()); 
} 

由於記錄是在安裝程序中創建的,它們保留在測試失敗的數據庫中。對此有何建議?你的解決方案如何?

回答

2

有沒有簡單的方法來做你所要求的。通常所做的是在執行測試之前和之後截斷數據庫,以便您擁有一個真正乾淨且隔離的環境。

從這個漂亮的文章引用(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")); 
} 

正如你所看到的,該解決方案利用主義的Symfony2中,以達到隔離狀態命令有一個包,我喜歡用它解決。正是這個問題,讓你用好後就可以使用FunctionalTest基類和其他許多功能了。查看詳情:

https://github.com/liip/LiipFunctionalTestBundle