2009-04-23 45 views
1

我使用PHPUnit DataBase来测试一些使用MDB2的类。如何使用PHPUnit DataBase使用MDB2进行多个测试?

一切都很好,因为我遇到的第二个测试,它会返回一个错误:

Caught exception: Object of class MDB2_Error could not be converted to string

当我把第二个测试中发生的第一个,新的第一个测试是OK,但第二一个返回相同的错误! 还有下一个!

也许MDB2连接在第一次测试后关闭?

这里是我的构造函数:

public function __construct() 
{ 
    $this->pdo = new PDO('connectionstring', 'user', 'passwd'); 
    try { 
     $this->mdb2 = new MyDBA($this->dsn); 
    } 
    catch (Exception $e) { 
     error_log(' __construct Caught exception: '.$e->getMessage()); 
    } 
} 

MyDBA返回一个单例。 没有引发异常的构造函数里面...

下面是这两个第一次测试:

public function testTranslationAdd() 
{ 
    try { 
     $id = $this->mdb2->addTranslation("This is the second english translation.","en"); 
    } 
    catch (Exception $e) { 
     error_log(' testTranslationAdd Caught exception: '.$e->getMessage()); 
    } 

    $xml_dataset = $this->createFlatXMLDataSet(dirname(__FILE__).'/state/twotranslations.xml'); 
    $this->assertDataSetsEqual($xml_dataset, 
           $this->getConnection()->createDataSet(array("translation"))); 
} 

public function testTranslationGet() 
{ 
    try { 
     $text = $this->mdb2->getTranslation(1,"en"); 
    } 
    catch (Exception $e) { 
     error_log(' testTranslationGet Caught exception: '.$e->getMessage()); 
    } 

    $this->assertEquals("This is the first english translation.",$text); 
} 
+0

我很困惑,抛出的异常在哪里? – james 2011-04-23 23:53:09

回答

2

你真的应该添加断言您MDB2的结果是没有错误:

$this->assertFalse(MDB2::isError($this->mdb2), 'MDB2 error'); 

那不幸的是,不会给你提示错误是什么,如果你没有错误,直接使用getMessage()将会失败。那为什么你应该这样写:

if (MDB2::isError($this->mdb2)) { 
    $this->fail('MDB2 error: ' . $this->mdb2->getMessage()); 
} 
相关问题