2016-02-26 14 views
1

我發現例如如何在手動PHPUnit的,使用其他的固定裝置在yii1

class CommentTest extends CDbTestCase 
{ 
    public $fixtures=array(
     'posts'=>'Post', 
     'comments'=>'Comment', 
    ); 

    … 
} 

使用夾具或我可以使用像這樣的「後」 =>「:交」。 但我想在其他測試中使用其他燈具,例如:當我測試模型時,我使用「post.php」燈具,當我測試模型作者我想用post_for_author.php(數據必須插入表後)

我嘗試寫這樣的:

class CommentTest extends CDbTestCase 
{ 
    public $fixtures=array(
     'posts_for_author'=>'Post', 
     'comments'=>'Comment', 
    ); 

    … 
} 

等方式 'posts_for_author'=> ':後', 但它不工作。請幫幫我,怎麼辦?

回答

1

使用像這樣的隔離裝置。

應用/測試/單位:

class CommentTest extends IsolatedFixtureDbTestCase { 
     public $fixtures=array(
      'posts_for_author'=>'Post', 
      'comments'=>'Comment', 
     ); 
     ... 
    } 

應用/測試:

abstract class IsolatedFixtureDbTestCase extends CDbTestCase 
{ 
    private $basePathOld = null; 

    public function setUp() 
    { 
     $fixturePath = Yii::getPathOfAlias('application.tests.fixtures.' . get_class($this)); 
     if (is_dir($fixturePath)) { 
      $this->basePathOld = $this->getFixtureManager()->basePath; 
      $this->getFixtureManager()->basePath = $fixturePath; 
     } 
     parent::setUp(); 
    } 

    public function tearDown() 
    { 
     parent::tearDown(); 
     if (null !== $this->basePathOld) { 
      $this->getFixtureManager()->basePath = $this->basePathOld; 
     } 
    } 
} 

,然後在應用/測試/燈具,您可以創建文件夾CommentTest,有您的燈具對於這一類

+0

再次謝謝! – ConorHolt