我写一个基本的PDO包装类,当我想以模拟异常抛出的PDOStatement::prepare()使用willThrowException()与模拟PDOException在我的单元测试,返回的值getMessage()始终是空字符串,而不是我的设置。
这是我尝试的方法:
// WrapperClass.php
<?php
class WrapperClass
{
private $pdo;
private $error = '';
public function __construct(\PDO $pdo)
{
$this->pdo = $pdo;
}
public function save()
{
$sql = 'INSERT INTO ...';
try {
$this->pdo->prepare($sql);
// some value binding and executing the statement
} catch (\PDOException $pdoException) {
$this->error = $pdoException->getMessage();
}
}
public function getError()
{
return $this->error;
}
}
Run Code Online (Sandbox Code Playgroud)
和我的测试:
// WrapperClassTest.php
<?php
class WrapperClassTest extends \PHPUnit_Framework_TestCase
{
/**
* …Run Code Online (Sandbox Code Playgroud)