我得到一个致命的错误:在PHPUnit测试中调用未定义的方法PHPUnit_Extensions_Database_DefaultDatabaseConnection::prepare(),我正在准备一个PDO语句。如果默认的数据库连接是成功连接的pdo对象的副本,那么它就不能访问它的方法吗?
我的课程和所讨论的函数:
class User
{
protected $db;
public function __construct($db)
{
$this->db = $db;
}
public function deleteItem($itemId)
{
$sql = "
DELETE FROM Users WHERE id = ?";
$sth = $this->db->prepare($sql);//this is the failed line works on other tests
return $sth->execute(array($itemId));
}
我的测试:
class RosterDBTest extends PHPUnit_Extensions_Database_Testcase
{
public function getConnection()
{
$pdo = new PDO('mysql:host=localhost;dbname=users','root','root');
return $this->createDefaultDBConnection($pdo,"users");
}
public function getDataSet()
{
return $this->createFlatXMLDataset(
dirname(__FILE__) . '/users.xml');
}
public function setup()
{
$this->db = $this->getConnection();
}
public function testRemoveUser()
{
$testUser = new User($this->db);
$expectedUsers = 123;
$testUser->deleteItem(91);
$totalUsers = $testUsers->getAllUsers();
$this->assertEquals( $expectedUsers,count($totalUsers), 'Did not delete User 91' );
}
最佳答案
我刚刚遇到了一个类似的问题,我有一个抽象的数据库测试用例类。通过改变来解决它
public function setup()
{
$this->db = $this->getConnection();
}
到
public function setup()
{
$this->db = $this->getConnection()->getConnection();
}
不确定这是否有帮助,但希望能帮助某人。
关于php - PHPUnit_Extensions_Database_Testcase返回未定义的方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25901470/