如何使用带有PHPUnit数据库的MDB2进行多项测试?

如何使用带有PHPUnit数据库的MDB2进行多项测试?,php,phpunit,pear,mdb2,Php,Phpunit,Pear,Mdb2,我使用PHPUnit数据库使用MDB2测试一些类 一切正常,因为我遇到了第二个测试,它返回一个错误: 捕获的异常:类的对象 无法将MDB2_错误转换为 串 当我放置第二个测试代替第一个测试时,新的第一个测试是可以的,但是第二个测试返回相同的错误! 还有下一个 第一次测试后,MDB2连接可能已关闭 这是我的构造函数: public function __construct() { $this->pdo = new PDO('connectionstring', 'user', 'pa

我使用PHPUnit数据库使用MDB2测试一些类

一切正常,因为我遇到了第二个测试,它返回一个错误:

捕获的异常:类的对象 无法将MDB2_错误转换为 串

当我放置第二个测试代替第一个测试时,新的第一个测试是可以的,但是第二个测试返回相同的错误! 还有下一个

第一次测试后,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);
}

您确实应该添加mdb2结果没有错误的断言:

$this->assertFalse(MDB2::isError($this->mdb2), 'MDB2 error');
不幸的是,这并没有给您任何错误提示,如果没有错误,直接在断言中使用
getMessage()
,将严重失败。这就是为什么你应该这样写:

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

我很困惑,异常在哪里抛出?