我使用PHPUnit DataBase使用MDB2测试某个类。
一切都很好,因为我遇到了第二个测试,它返回一个错误:
捕获异常:类的对象 MDB2_Error无法转换为 串
当我将第二个测试放在第一个测试的位置时,新的第一个测试是正常的,但第二个测试返回相同的错误! 还有下一个!
第一次测试后,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 :(得分:2)
你应该添加断言你的mdb2结果没有错误:
$this->assertFalse(MDB2::isError($this->mdb2), 'MDB2 error');
遗憾的是,没有给出任何提示错误,并且如果没有错误,在断言中直接使用getMessage()
将会失败。这就是为什么你应该这样写的东西:
if (MDB2::isError($this->mdb2)) {
$this->fail('MDB2 error: ' . $this->mdb2->getMessage());
}